}
pub fn location_nth_vaporized_asteroid(pos: (i32, i32), map: &[(i32, i32)], n: usize) -> (i32, i32) {
- // Angle -> []
+ // Angle -> [(position, distance)].
let mut asteroids = HashMap::<i64, Vec<((i32, i32), i64)>>::new();
let (x1, y1) = pos;
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 {
..#.#.....#....##";
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() {
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
match self.next_command {\r
NextCommand::ColorToPaint => { self.panels.insert(self.current_pos, value); NextCommand::Turn },\r
NextCommand::Turn => {\r
- self.current_dir = (self.current_dir + if value == 0 { 3 } else { 1 }) % 4;\r
+ self.current_dir = (self.current_dir + if value == 0 /* Turn left. */ { 3 } else /* Turn right. */ { 1 }) % 4;\r
+ let (x, y) = self.current_pos;\r
self.current_pos =\r
match self.current_dir {\r
- 0 => (self.current_pos.0, self.current_pos.1 + 1),\r
- 1 => (self.current_pos.0 + 1, self.current_pos.1),\r
- 2 => (self.current_pos.0, self.current_pos.1 - 1),\r
- 3 | _ => (self.current_pos.0 - 1, self.current_pos.1)\r
+ 0 => (x , y + 1),\r
+ 1 => (x + 1, y ),\r
+ 2 => (x , y - 1),\r
+ 3 | _ => (x - 1, y )\r
};\r
NextCommand::ColorToPaint\r
}\r
\r
assert_eq!(robot.panels.len(), 6);\r
}\r
-\r
}
\ No newline at end of file
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 {