From 55ad169ce1280d592c1f50594fb1a590ba72e5da Mon Sep 17 00:00:00 2001 From: =?utf8?q?Gr=C3=A9gory=20Burri?= Date: Thu, 12 Dec 2019 07:50:43 +0100 Subject: [PATCH] Day 10 part 2 --- src/day10.rs | 19 +++++++------------ src/day11.rs | 12 ++++++------ src/main.rs | 4 +++- 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/src/day10.rs b/src/day10.rs index 9b8aebb..6b0af5b 100644 --- a/src/day10.rs +++ b/src/day10.rs @@ -42,7 +42,7 @@ pub fn find_best_location(map: &[(i32, i32)]) -> (usize, (i32, i32)) { } pub fn location_nth_vaporized_asteroid(pos: (i32, i32), map: &[(i32, i32)], n: usize) -> (i32, i32) { - // Angle -> [] + // Angle -> [(position, distance)]. let mut asteroids = HashMap::>::new(); let (x1, y1) = pos; @@ -60,10 +60,6 @@ pub fn location_nth_vaporized_asteroid(pos: (i32, i32), map: &[(i32, i32)], n: u asteroids.values_mut().for_each(|lineup_asteroids| lineup_asteroids.sort_by(|(_, l1), (_, l2)| l1.cmp(l2))); - dbg!(&sorted_angles); - dbg!(&asteroids); - //return (0, 0); - let mut i = 1; loop { for angle in &sorted_angles { @@ -185,11 +181,12 @@ mod tests { ..#.#.....#....##"; let map = read_map(raw_map); let pos = (8, 3); - let pos_200th = location_nth_vaporized_asteroid(pos, &map, 200); - dbg!(pos_200th); - assert_eq!(2, 210); + let pos_9th = location_nth_vaporized_asteroid(pos, &map, 9); + assert_eq!(pos_9th, (15, 1)); + + let pos_18th = location_nth_vaporized_asteroid(pos, &map, 18); + assert_eq!(pos_18th, (4, 4)); } - /* #[test] fn part2_sample_2() { @@ -217,8 +214,6 @@ mod tests { let map = read_map(raw_map); let pos = find_best_location(&map).1; let pos_200th = location_nth_vaporized_asteroid(pos, &map, 200); - dbg!(pos_200th); - assert_eq!(2, 210); + assert_eq!(pos_200th, (8, 2)); } - */ } \ No newline at end of file diff --git a/src/day11.rs b/src/day11.rs index e91d813..cb3c154 100644 --- a/src/day11.rs +++ b/src/day11.rs @@ -34,13 +34,14 @@ impl intcode::IO for Robot { match self.next_command { NextCommand::ColorToPaint => { self.panels.insert(self.current_pos, value); NextCommand::Turn }, NextCommand::Turn => { - self.current_dir = (self.current_dir + if value == 0 { 3 } else { 1 }) % 4; + self.current_dir = (self.current_dir + if value == 0 /* Turn left. */ { 3 } else /* Turn right. */ { 1 }) % 4; + let (x, y) = self.current_pos; self.current_pos = match self.current_dir { - 0 => (self.current_pos.0, self.current_pos.1 + 1), - 1 => (self.current_pos.0 + 1, self.current_pos.1), - 2 => (self.current_pos.0, self.current_pos.1 - 1), - 3 | _ => (self.current_pos.0 - 1, self.current_pos.1) + 0 => (x , y + 1), + 1 => (x + 1, y ), + 2 => (x , y - 1), + 3 | _ => (x - 1, y ) }; NextCommand::ColorToPaint } @@ -112,5 +113,4 @@ mod tests { assert_eq!(robot.panels.len(), 6); } - } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index c025b79..18d560e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -73,7 +73,9 @@ fn day09() -> String { fn day10() -> String { let map = day10::read_map(&fs::read_to_string("data/day10.input").unwrap()); - format!("part1: {}, part2: {}", day10::find_best_location(&map).0, "") + let (n, location) = day10::find_best_location(&map); + let (x, y) = day10::location_nth_vaporized_asteroid(location, &map, 200); + format!("part1: {}, part2: {}", n, x * 100 + y) } fn day11() -> String { -- 2.45.2