From 87a16eff473605023f623c561e6ae8f82e0fed2b Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Fri, 9 Dec 2022 14:10:28 +0100 Subject: [PATCH] Day[9] --- data/day09.input | 2000 ++++++++++++++++++++++++++++++++++++++++++++++ src/day09.rs | 112 +++ src/main.rs | 7 + 3 files changed, 2119 insertions(+) create mode 100644 data/day09.input create mode 100644 src/day09.rs diff --git a/data/day09.input b/data/day09.input new file mode 100644 index 0000000..1ffd083 --- /dev/null +++ b/data/day09.input @@ -0,0 +1,2000 @@ +L 2 +D 2 +R 2 +U 1 +L 1 +U 1 +R 1 +U 2 +R 2 +U 2 +L 2 +R 2 +U 2 +L 2 +U 1 +R 1 +U 1 +R 2 +U 1 +D 2 +U 2 +L 2 +R 1 +U 2 +R 2 +U 2 +L 1 +D 1 +L 1 +R 2 +L 2 +D 1 +L 1 +R 2 +U 2 +L 1 +U 1 +L 2 +R 1 +L 1 +D 2 +U 2 +L 2 +D 1 +R 2 +D 1 +U 2 +D 2 +L 2 +D 1 +L 1 +R 1 +D 2 +U 2 +L 2 +U 1 +L 1 +U 2 +R 2 +D 1 +U 2 +R 2 +U 1 +L 2 +R 2 +D 2 +U 2 +D 2 +L 2 +D 1 +U 2 +L 1 +R 1 +L 1 +D 2 +U 1 +D 1 +L 2 +D 1 +L 1 +R 1 +U 1 +L 2 +R 2 +U 2 +D 2 +L 1 +R 2 +D 1 +R 2 +D 2 +L 1 +U 2 +R 1 +D 2 +L 1 +D 2 +U 2 +D 1 +U 2 +R 1 +D 1 +L 1 +D 2 +R 2 +U 1 +R 1 +D 2 +L 2 +R 2 +D 1 +R 3 +U 1 +D 1 +U 1 +R 2 +U 2 +R 3 +U 3 +L 3 +D 2 +L 3 +D 1 +L 3 +D 3 +R 3 +D 3 +R 3 +L 2 +D 3 +R 1 +L 3 +U 1 +L 1 +U 1 +L 2 +R 3 +D 2 +U 3 +R 3 +D 2 +L 3 +R 3 +D 1 +L 3 +U 1 +D 1 +L 1 +D 2 +L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 +U 13 +R 9 +L 5 +D 10 +U 18 +D 13 +U 13 +D 14 +L 3 +R 2 +L 13 +D 11 +R 17 +L 15 +D 3 +L 1 +U 10 +L 14 +D 15 +L 13 +R 12 +D 4 +L 1 +D 2 +R 16 +D 10 +U 11 +L 12 +U 1 +D 2 +R 3 +U 2 +R 12 +L 6 +D 1 +L 13 +R 11 +U 8 +L 8 +D 10 +R 5 +U 6 +L 7 +R 4 +U 9 +L 6 +D 10 +L 14 +U 11 +D 1 +L 7 +R 3 +U 16 +L 11 +D 13 +L 14 +U 10 +L 3 +R 12 +U 9 +R 11 +U 10 +L 8 +D 5 +U 9 +L 16 +R 17 +D 3 +R 5 +D 13 +U 8 +R 5 +U 16 +R 2 +U 13 +D 16 +L 9 +D 14 +L 15 +U 15 +L 1 +D 8 +L 7 +D 1 +R 11 +D 16 +R 9 +U 14 +L 18 +U 4 +D 14 +L 14 +D 9 +R 6 +L 1 +R 4 +L 11 +D 17 +L 9 +R 10 +L 19 +U 12 +D 15 +R 9 +L 3 +D 13 +R 6 +D 19 +R 9 +D 1 +L 13 +D 8 +L 19 +U 14 +R 18 +D 10 +R 7 +L 18 +U 18 +D 5 +L 1 +U 10 +D 10 +U 19 +R 14 +L 7 +D 19 +U 10 +L 3 +U 17 +D 1 +L 5 +U 7 +R 16 +D 3 +U 8 +D 17 +U 17 +D 17 +L 14 +D 19 +R 8 +D 14 +L 7 +U 7 +L 5 +D 6 +L 1 +U 14 +L 10 +U 15 +L 7 +D 14 +L 11 +R 5 +D 14 +L 13 +R 2 +L 19 +D 7 +R 8 +L 16 +U 4 +L 9 +U 17 +R 15 +L 8 +U 15 +D 5 +R 8 +L 16 +R 6 +D 17 +U 19 +D 19 +R 10 +D 4 +U 12 +D 2 +L 18 +D 13 +L 7 +U 17 +L 13 +D 5 +U 2 +L 6 +R 4 +U 19 +L 4 +D 8 +U 7 +L 3 +R 4 +U 17 +L 1 +U 6 +R 9 +D 16 +R 4 +D 9 +R 9 +L 11 +U 17 +D 18 +U 18 +R 16 +L 7 +R 18 +U 5 +L 4 +D 7 \ No newline at end of file diff --git a/src/day09.rs b/src/day09.rs new file mode 100644 index 0000000..106b947 --- /dev/null +++ b/src/day09.rs @@ -0,0 +1,112 @@ +use std::{cmp::max, collections::HashSet}; + +enum Direction { + Left, + Up, + Right, + Down, +} + +pub struct Movement { + direction: Direction, + distance: i32, +} + +pub fn parse(input: &str) -> Vec { + input.lines().map(|l| { + let split: Vec<&str> = l.trim().split(' ').collect(); + Movement { + direction: match split[0] { + "L" => Direction::Left, + "U" => Direction::Up, + "R" => Direction::Right, + "D" => Direction::Down, + other => panic!("Uknown movement: {}", other), + }, + distance: split[1].parse().expect("Can't parse distance"), + } + }).collect() +} + +fn chebyshev_distance(p1: (i32, i32), p2: (i32, i32)) -> i32 { + max((p2.0 - p1.0).abs(), (p2.1 - p1.1).abs()) +} + +pub fn nb_positions_visited_by_tail(movements: &[Movement]) -> usize { + let mut visited: HashSet<(i32, i32)> = HashSet::new(); + visited.insert((0, 0)); + + let mut rope = [(0, 0); N]; // First element is the tail, last element is the head. + + for m in movements { + for _ in 0..m.distance { + // 1) Move the head. + let h = &mut rope[N-1]; + *h = match m.direction { + Direction::Left => (h.0 - 1, h.1), + Direction::Up => (h.0, h.1 - 1), + Direction::Right => (h.0 + 1, h.1), + Direction::Down => (h.0, h.1 + 1), + }; + + // 2) Move the rest of the rope. + for i in (0..N-1).rev() { + let target = rope[i+1]; + let mut node = &mut rope[i]; + if chebyshev_distance(*node, target) >= 2 { + let (dx, dy) = (node.0 - target.0, node.1 - target.1); + (node.0, node.1) = + if dx.abs() > dy.abs() { + (target.0 + dx.signum(), target.1) + } else if dx.abs() < dy.abs() { + (target.0, target.1 + dy.signum()) + } else { + (target.0 + dx.signum(), target.1 + dy.signum()) + }; + if i == 0 { + visited.insert(*node); + } + } + } + } + }; + visited.len() +} + +#[cfg(test)] +mod tests { + use super::*; + + static MOVEMENTS: &str = + "R 4 + U 4 + L 3 + D 1 + R 4 + D 1 + L 5 + R 2"; + + #[test] + fn part1() { + let movements = parse(MOVEMENTS); + assert_eq!(nb_positions_visited_by_tail::<2>(&movements), 13); + } + + #[test] + fn part2() { + let movements = parse(MOVEMENTS); + assert_eq!(nb_positions_visited_by_tail::<10>(&movements), 1); + + let movements_2 = parse( + "R 5 + U 8 + L 8 + D 3 + R 17 + D 10 + L 25 + U 20"); + assert_eq!(nb_positions_visited_by_tail::<10>(&movements_2), 36); + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 28b0f67..44cbcd1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,7 @@ mod day05; mod day06; mod day07; mod day08; +mod day09; fn day01() -> String { let f = fs::File::open("data/day01.input").unwrap(); @@ -70,6 +71,11 @@ fn day08() -> String { format!("part1: {}, part2: {}", day08::number_of_visible_trees(&forest), day08::best_scenic_score(&forest)) } +fn day09() -> String { + let movements = day09::parse(&fs::read_to_string("data/day09.input").unwrap()); + format!("part1: {}, part2: {}", day09::nb_positions_visited_by_tail::<2>(&movements), day09::nb_positions_visited_by_tail::<10>(&movements)) +} + fn format_micros(t: u128) -> String { if t < 10_000 { format!("{} μs", t) @@ -97,6 +103,7 @@ fn main() { day06, day07, day08, + day09, ); let args: Vec = env::args().skip(1).collect(); -- 2.45.2