1
0
Fork 0

porting over AOC from previous years to a monorepo.

This commit is contained in:
Gabe Venberg 2026-04-16 14:45:29 +02:00
commit 84c4cf9991
194 changed files with 30104 additions and 0 deletions

11
2022/days/09/Cargo.toml Normal file
View file

@ -0,0 +1,11 @@
[package]
name = "day09"
authors.workspace = true
description.workspace = true
version.workspace = true
edition.workspace = true
[dependencies]
aoc_libs.workspace = true
regex.workspace = true
once_cell.workspace = true

2000
2022/days/09/src/input.txt Normal file

File diff suppressed because it is too large Load diff

15
2022/days/09/src/main.rs Normal file
View file

@ -0,0 +1,15 @@
mod part1;
mod part2;
mod parse;
mod rope;
fn main() {
let input = include_str!("./input.txt");
let structured_input = parse::parse(input);
println!("Part One");
println!("Result: {}", part1::part1(&structured_input));
println!("Part Two");
println!("Result: {}", part2::part2(&structured_input));
}

75
2022/days/09/src/parse.rs Normal file
View file

@ -0,0 +1,75 @@
use once_cell::sync::Lazy;
use regex::Regex;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Direction {
Up,
Right,
Down,
Left,
}
static PARSE_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"^([URDL]) (\d+)$").unwrap());
pub fn parse(input: &str) -> Vec<Direction> {
let mut ret = Vec::new();
for line in input.lines() {
let captures = PARSE_REGEX
.captures(line)
.unwrap_or_else(|| panic!("invalid line {}", line));
let dir = match &captures[1] {
"U" => Direction::Up,
"R" => Direction::Right,
"D" => Direction::Down,
"L" => Direction::Left,
_ => panic!("invalid direction char"),
};
for _ in 0..captures[2]
.parse()
.unwrap_or_else(|_| panic!("invalid line {}", line))
{
ret.push(dir)
}
}
ret
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse() {
let input =
concat!("R 4\n", "U 4\n", "L 3\n", "D 1\n", "R 4\n", "D 1\n", "L 5\n", "R 2\n",);
assert_eq!(
parse(input),
vec![
Direction::Right,
Direction::Right,
Direction::Right,
Direction::Right,
Direction::Up,
Direction::Up,
Direction::Up,
Direction::Up,
Direction::Left,
Direction::Left,
Direction::Left,
Direction::Down,
Direction::Right,
Direction::Right,
Direction::Right,
Direction::Right,
Direction::Down,
Direction::Left,
Direction::Left,
Direction::Left,
Direction::Left,
Direction::Left,
Direction::Right,
Direction::Right,
]
);
}
}

53
2022/days/09/src/part1.rs Normal file
View file

@ -0,0 +1,53 @@
use std::collections::HashSet;
use aoc_libs::points::Point;
use crate::parse::Direction;
use crate::rope::Rope;
pub fn part1(input: &Vec<Direction>) -> usize {
let mut visited: HashSet<Point> = HashSet::new();
let mut rope: Rope<2> = Rope::new();
visited.insert(*rope.get_tail_pos());
for direction in input {
rope.update_rope(direction);
visited.insert(*rope.get_tail_pos());
}
visited.len()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part1() {
let input = vec![
Direction::Right,
Direction::Right,
Direction::Right,
Direction::Right,
Direction::Up,
Direction::Up,
Direction::Up,
Direction::Up,
Direction::Left,
Direction::Left,
Direction::Left,
Direction::Down,
Direction::Right,
Direction::Right,
Direction::Right,
Direction::Right,
Direction::Down,
Direction::Left,
Direction::Left,
Direction::Left,
Direction::Left,
Direction::Left,
Direction::Right,
Direction::Right,
];
assert_eq!(part1(&input), 13);
}
}

31
2022/days/09/src/part2.rs Normal file
View file

@ -0,0 +1,31 @@
use std::collections::HashSet;
use aoc_libs::points::Point;
use crate::{parse::Direction, rope::Rope};
pub fn part2(input: &Vec<Direction>) -> usize {
let mut visited: HashSet<Point> = HashSet::new();
let mut rope: Rope<10> = Rope::new();
visited.insert(*rope.get_tail_pos());
for direction in input {
rope.update_rope(direction);
visited.insert(*rope.get_tail_pos());
}
visited.len()
}
#[cfg(test)]
mod tests {
use crate::parse::parse;
use super::*;
#[test]
fn test_part2() {
let input = parse(concat!(
"R 5\n", "U 8\n", "L 8\n", "D 3\n", "R 17\n", "D 10\n", "L 25\n", "U 20\n",
));
assert_eq!(part2(&input), 36);
}
}

160
2022/days/09/src/rope.rs Normal file
View file

@ -0,0 +1,160 @@
use std::fmt::Display;
use crate::parse::Direction;
use aoc_libs::points::{Point, UPoint};
// L is the length of the rope in segments.
#[derive(Debug)]
pub struct Rope<const L: usize> {
segments: [Point; L],
}
impl<const L: usize> Rope<L> {
pub fn new() -> Rope<L> {
Rope {
segments: [Point::default(); L],
}
}
pub fn update_rope(&mut self, direction: &Direction) {
self.segments[0] += match direction {
Direction::Up => Point { x: 0, y: 1 },
Direction::Right => Point { x: 1, y: 0 },
Direction::Down => Point { x: 0, y: -1 },
Direction::Left => Point { x: -1, y: 0 },
};
for segment in 1..self.segments.len() {
self.segments[segment] += Rope::<L>::update_single_segment_pair(
&self.segments[segment - 1],
&self.segments[segment],
)
}
}
pub fn get_tail_pos(&self) -> &Point {
&self.segments[self.segments.len() - 1]
}
// the rope segment will not move if the segment ahead of it is only at most one away, and will
// move with the following rules if it is 2 away: It moves straight towards the head if the
// head is directly up/down/left/right, and diagonally towards it if its not straight
// up/down/left/right.
fn update_single_segment_pair(head: &Point, tail: &Point) -> Point {
let delta = *head - *tail;
if delta.x.abs() > 2 || delta.y.abs() > 2 {
panic!("invalid delta ({}, {})", delta.y, delta.x)
}
match (delta.x, delta.y) {
(0, 2) => Point { x: 0, y: 1 },
(2, 0) => Point { x: 1, y: 0 },
(0, -2) => Point { x: 0, y: -1 },
(-2, 0) => Point { x: -1, y: 0 },
(1, 2) | (2, 2) | (2, 1) => Point { x: 1, y: 1 },
(2, -1) | (2, -2) | (1, -2) => Point { x: 1, y: -1 },
(-1, -2) | (-2, -2) | (-2, -1) => Point { x: -1, y: -1 },
(-2, 1) | (-2, 2) | (-1, 2) => Point { x: -1, y: 1 },
_ => Point { x: 0, y: 0 },
}
}
}
impl<const L: usize> Display for Rope<{ L }> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let grid_size = (self.segments.len() * 2) - 1;
let zero_point = UPoint {
x: self.segments.len() - 1,
y: self.segments.len() - 1,
};
let mut grid: Vec<Vec<char>> = Vec::with_capacity(grid_size);
for y in 0..grid_size {
grid.push(Vec::with_capacity(grid_size));
for _ in 0..grid_size {
grid[y].push('.')
}
}
for segment in self.segments.iter().skip(1) {
let delta = *segment - self.segments[0];
let upoint = delta.to_upoint(&zero_point).unwrap();
grid[upoint.y][upoint.x] = 'T'
}
writeln!(
f,
"head is at {}, {}",
self.segments[0].x, self.segments[0].y
)?;
let upoint = Point { x: 0, y: 0 }.to_upoint(&zero_point).unwrap();
grid[upoint.y][upoint.x] = 'H';
for line in grid {
let mut writeline = "".to_string();
for char in line {
writeline.push(char)
}
writeln!(f, "{}", writeline)?;
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use std::collections::HashSet;
use super::*;
#[test]
fn test_single_segment() {
let input = [
Direction::Right,
Direction::Right,
Direction::Right,
Direction::Right,
Direction::Up,
Direction::Up,
Direction::Up,
Direction::Up,
Direction::Left,
Direction::Left,
Direction::Left,
Direction::Down,
Direction::Right,
Direction::Right,
Direction::Right,
Direction::Right,
Direction::Down,
Direction::Left,
Direction::Left,
Direction::Left,
Direction::Left,
Direction::Left,
Direction::Right,
Direction::Right,
];
let mut visited: HashSet<Point> = HashSet::new();
let mut rope: Rope<2> = Rope::new();
visited.insert(*rope.get_tail_pos());
println!("{}", rope);
for direction in input {
rope.update_rope(&direction);
visited.insert(*rope.get_tail_pos());
println!("{}", rope);
}
// let mut graph = [
// ['.', '.', '.', '.', '.', '.'],
// ['.', '.', '.', '.', '.', '.'],
// ['.', '.', '.', '.', '.', '.'],
// ['.', '.', '.', '.', '.', '.'],
// ['s', '.', '.', '.', '.', '.'],
// ];
// for point in &visited {
// graph[4 - point.y as usize][point.x as usize] = '#';
// }
// for line in graph {
// println!("{:?}", line)
// }
assert_eq!(visited.len(), 13)
}
}