From 5c6facfa6668118754860d98e0e9ec3cd5cd9fa9 Mon Sep 17 00:00:00 2001 From: mat ess Date: Fri, 9 Dec 2022 14:46:59 -0500 Subject: [PATCH] Day 9 --- inputs/9.small.txt | 8 + inputs/9.txt | 2000 ++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 125 ++- 3 files changed, 2132 insertions(+), 1 deletion(-) create mode 100644 inputs/9.small.txt create mode 100644 inputs/9.txt diff --git a/inputs/9.small.txt b/inputs/9.small.txt new file mode 100644 index 0000000..9874df2 --- /dev/null +++ b/inputs/9.small.txt @@ -0,0 +1,8 @@ +R 4 +U 4 +L 3 +D 1 +R 4 +D 1 +L 5 +R 2 diff --git a/inputs/9.txt b/inputs/9.txt new file mode 100644 index 0000000..949fccd --- /dev/null +++ b/inputs/9.txt @@ -0,0 +1,2000 @@ +L 2 +D 1 +L 2 +U 2 +R 1 +D 2 +U 1 +R 2 +U 2 +L 1 +D 2 +L 1 +D 2 +L 2 +U 2 +D 2 +L 2 +R 1 +U 1 +D 2 +U 1 +L 1 +U 2 +D 2 +L 2 +D 2 +L 2 +R 2 +D 2 +U 2 +R 2 +U 2 +R 2 +U 1 +R 2 +D 2 +R 2 +D 1 +U 1 +L 1 +U 2 +R 1 +D 2 +U 1 +L 2 +D 2 +U 2 +L 1 +R 2 +D 2 +U 2 +D 1 +L 1 +U 2 +D 2 +L 2 +D 2 +R 2 +L 2 +R 2 +L 1 +U 2 +R 1 +D 1 +L 2 +U 1 +R 2 +U 1 +D 1 +U 2 +R 2 +U 2 +R 2 +D 2 +U 1 +D 2 +R 1 +U 1 +D 1 +R 1 +U 2 +L 2 +U 2 +R 1 +D 1 +R 2 +D 1 +L 2 +U 2 +R 2 +L 1 +R 2 +D 1 +U 2 +D 2 +L 1 +U 1 +R 2 +D 1 +U 2 +L 2 +U 2 +D 2 +L 2 +D 2 +R 1 +L 2 +U 2 +R 1 +D 1 +L 1 +D 3 +U 2 +D 2 +R 1 +L 3 +R 1 +U 2 +R 1 +L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 +D 3 +U 16 +L 3 +D 6 +U 3 +L 2 +R 3 +L 1 +R 6 +L 14 +U 10 +L 13 +R 8 +U 3 +D 7 +U 9 +D 2 +L 14 +D 5 +R 7 +D 8 +L 18 +R 13 +U 14 +D 6 +R 10 +L 10 +U 2 +L 11 +R 12 +U 6 +R 16 +L 10 +U 7 +R 12 +L 15 +D 2 +U 16 +L 4 +D 2 +L 1 +R 8 +D 7 +L 11 +R 4 +D 13 +U 13 +R 1 +D 15 +L 12 +R 5 +D 10 +U 8 +R 14 +L 10 +U 9 +R 7 +U 13 +L 5 +U 9 +L 15 +R 4 +D 13 +R 12 +D 5 +R 18 +L 3 +D 13 +R 17 +D 9 +L 8 +D 3 +L 5 +D 8 +U 10 +D 8 +L 2 +U 10 +R 6 +D 13 +L 17 +R 17 +D 16 +R 14 +L 6 +U 17 +R 9 +D 11 +L 7 +U 16 +R 16 +L 1 +R 17 +D 9 +L 6 +D 16 +R 12 +L 7 +U 5 +D 14 +R 8 +U 12 +L 12 +R 8 +U 17 +L 13 +D 17 +L 15 +R 7 +U 11 +L 10 +R 8 +U 14 +R 6 +U 11 +D 17 +U 13 +L 12 +U 11 +L 16 +D 13 +U 11 +L 2 +R 11 +L 13 +U 12 +R 15 +U 17 +L 10 +U 17 +D 2 +R 1 +L 10 +R 10 +U 14 +L 12 +U 8 +R 4 +L 12 +U 13 +L 2 +R 9 +D 14 +U 17 +L 16 +D 9 +R 3 +L 17 +U 8 +R 16 +D 11 +L 1 +U 15 +L 19 +R 8 +D 11 +R 14 +D 19 +R 12 +U 3 +L 9 +U 2 +L 6 +D 6 +U 16 +D 4 +L 1 +D 10 +R 8 +U 17 +D 2 +R 6 +D 19 +U 6 +R 9 +U 17 +D 7 +L 15 +D 13 +L 17 +U 1 +R 17 +D 3 +L 15 +U 16 +D 13 +R 13 +D 14 +L 12 +D 4 +L 16 +U 15 +R 8 +U 19 +D 15 +L 5 +R 15 +L 19 +U 6 +D 6 +U 10 +L 1 +R 19 +L 1 +R 6 +L 1 +U 17 +R 10 +U 4 +D 5 +R 6 +U 3 +R 12 +U 13 +D 7 +R 15 +U 5 +R 2 +U 10 +L 13 +U 3 +D 5 +R 19 +U 9 +D 5 +L 3 +D 17 +L 16 +D 7 +L 16 +R 11 +D 3 +U 1 +R 2 +D 2 +L 4 +R 4 +U 17 +D 4 +L 10 +U 11 +D 13 diff --git a/src/main.rs b/src/main.rs index 12f3a85..f2f5551 100644 --- a/src/main.rs +++ b/src/main.rs @@ -212,7 +212,6 @@ solutions! { let map = map_trees(input); let len = map.len(); let mut count: u64 = 4 * len as u64 - 4; - // is_visible(&map, 2, 2, 5); for x in 1..len - 1 { for y in 1..len - 1 { if is_visible(&map, x, y, len) { @@ -237,9 +236,133 @@ solutions! { } max.into() }, + ], + [ + // day 9 part 1 + |input| { + let motions = parse_motions(input); + let visits = motion_visits(motions, 2); + (visits.len() as u64).into() + }, + // day 9 part 2 + |input| { + let motions = parse_motions(input); + let visits = motion_visits(motions, 10); + (visits.len() as u64).into() + }, ] } +#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +struct Pos { + x: isize, + y: isize, +} + +struct Rope { + knots: Vec, + tail: usize, +} + +impl Rope { + fn head(&mut self) -> &mut Pos { + &mut self.knots[0] + } + + fn tail(&mut self) -> &mut Pos { + &mut self.knots[self.tail] + } + + fn pull(&mut self) { + for i in 0..self.tail { + let head = self.knots[i]; + let tail = self.knots[i + 1]; + self.knots[i + 1] = move_tail(head, tail); + } + } +} + +fn move_tail(head: Pos, tail: Pos) -> Pos { + let diff_x = head.x - tail.x; + let diff_y = head.y - tail.y; + if diff_x.abs() < 2 && diff_y.abs() < 2 { + tail + } else if diff_y == 0 { + Pos { + x: tail.x + (diff_x / diff_x.abs()), + ..tail + } + } else if diff_x == 0 { + Pos { + y: tail.y + (diff_y / diff_y.abs()), + ..tail + } + } else { + Pos { + x: tail.x + (diff_x / diff_x.abs()), + y: tail.y + (diff_y / diff_y.abs()), + } + } +} + +fn motion_visits(motions: Vec, n_knots: usize) -> HashMap { + let mut map = HashMap::new(); + let pos = Pos { x: 0, y: 0 }; + let mut rope = Rope { + knots: vec![pos; n_knots], + tail: n_knots - 1, + }; + map.insert(pos, 1); + for motion in motions { + for _ in 0..motion.len { + match motion.dir { + Dir::U => rope.head().y += 1, + Dir::D => rope.head().y -= 1, + Dir::L => rope.head().x -= 1, + Dir::R => rope.head().x += 1, + } + rope.pull(); + if let Some(n) = map.get(&pos) { + map.insert(*rope.tail(), *n + 1); + } else { + map.insert(*rope.tail(), 1); + } + } + } + map +} + +fn parse_motions(input: String) -> Vec { + input + .lines() + .map(|line| { + let (dir, len) = line.split_once(' ').unwrap(); + let dir = match dir { + "U" => Dir::U, + "D" => Dir::D, + "L" => Dir::L, + "R" => Dir::R, + _ => unreachable!("ohno"), + }; + let len = len.parse::().unwrap(); + Motion { dir, len } + }) + .collect() +} + +struct Motion { + dir: Dir, + len: isize, +} + +#[derive(Debug)] +enum Dir { + U, + D, + L, + R, +} + fn view_length( me: u8, map: &[Vec],