|
| 1 | +use std::collections::HashSet; |
| 2 | + |
| 3 | +pub fn solve_16_01(raw_input: String) -> (i32, i32) { |
| 4 | + let input = process_input(&raw_input); |
| 5 | + |
| 6 | + let mut position = Position::new(); |
| 7 | + input.iter().for_each(|instruction| { |
| 8 | + position.step(instruction); |
| 9 | + }); |
| 10 | + |
| 11 | + let part_1 = position.location.manhattan_distance(); |
| 12 | + let part_2 = position.repeat.unwrap().manhattan_distance(); |
| 13 | + |
| 14 | + (part_1, part_2) |
| 15 | +} |
| 16 | + |
| 17 | +fn process_input(raw_input: &str) -> Vec<(Direction, i32)> { |
| 18 | + raw_input |
| 19 | + .trim() |
| 20 | + .split(", ") |
| 21 | + .map(|i| { |
| 22 | + let direction = match &i[0..=0] { |
| 23 | + "R" => Direction::Right, |
| 24 | + "L" => Direction::Left, |
| 25 | + value => panic!("Invalid instruction {value}"), |
| 26 | + }; |
| 27 | + let steps: i32 = i[1..].parse::<i32>().unwrap(); |
| 28 | + |
| 29 | + (direction, steps) |
| 30 | + }) |
| 31 | + .collect() |
| 32 | +} |
| 33 | + |
| 34 | +enum Facing { |
| 35 | + North, |
| 36 | + East, |
| 37 | + South, |
| 38 | + West, |
| 39 | +} |
| 40 | + |
| 41 | +enum Direction { |
| 42 | + Left, |
| 43 | + Right, |
| 44 | +} |
| 45 | + |
| 46 | +#[derive(Debug)] |
| 47 | +struct Point { |
| 48 | + pub x: i32, |
| 49 | + pub y: i32, |
| 50 | +} |
| 51 | + |
| 52 | +impl Point { |
| 53 | + pub fn manhattan_distance(&self) -> i32 { |
| 54 | + self.x.abs() + self.y.abs() |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +struct Position { |
| 59 | + facing: Facing, |
| 60 | + location: Point, |
| 61 | + visits: HashSet<(i32, i32)>, |
| 62 | + repeat: Option<Point>, |
| 63 | +} |
| 64 | + |
| 65 | +impl Position { |
| 66 | + pub fn new() -> Position { |
| 67 | + Position { |
| 68 | + facing: Facing::North, |
| 69 | + location: Point { x: 0, y: 0 }, |
| 70 | + visits: vec![(0, 0)].into_iter().collect(), |
| 71 | + repeat: Option::None, |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + pub fn step(&mut self, instruction: &(Direction, i32)) { |
| 76 | + let (direction, steps) = instruction; |
| 77 | + |
| 78 | + match (direction, &self.facing) { |
| 79 | + (Direction::Left, Facing::North) => self.move_to((-1, 0), *steps), |
| 80 | + (Direction::Left, Facing::East) => self.move_to((0, 1), *steps), |
| 81 | + (Direction::Left, Facing::South) => self.move_to((1, 0), *steps), |
| 82 | + (Direction::Left, Facing::West) => self.move_to((0, -1), *steps), |
| 83 | + (Direction::Right, Facing::North) => self.move_to((1, 0), *steps), |
| 84 | + (Direction::Right, Facing::East) => self.move_to((0, -1), *steps), |
| 85 | + (Direction::Right, Facing::South) => self.move_to((-1, 0), *steps), |
| 86 | + (Direction::Right, Facing::West) => self.move_to((0, 1), *steps), |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + fn move_to(&mut self, delta: (i32, i32), steps: i32) { |
| 91 | + let (x_diff, y_diff) = delta; |
| 92 | + |
| 93 | + self.facing = Self::facing_for_delta(delta); |
| 94 | + |
| 95 | + for _ in 0..steps { |
| 96 | + let Point { x, y } = self.location; |
| 97 | + self.location = match x_diff { |
| 98 | + 0 => Point { x, y: y + y_diff }, |
| 99 | + _ => Point { x: x + x_diff, y }, |
| 100 | + }; |
| 101 | + |
| 102 | + self.update_visits(); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + fn facing_for_delta(delta: (i32, i32)) -> Facing { |
| 107 | + match delta { |
| 108 | + (-1, 0) => Facing::West, |
| 109 | + (0, 1) => Facing::North, |
| 110 | + (1, 0) => Facing::East, |
| 111 | + (0, -1) => Facing::South, |
| 112 | + _ => unreachable!(), |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + fn update_visits(&mut self) { |
| 117 | + if self.repeat.is_none() { |
| 118 | + let Point { x, y } = self.location; |
| 119 | + if self.visits.contains(&(x, y)) { |
| 120 | + self.repeat = Some(Point { x, y }); |
| 121 | + } |
| 122 | + self.visits.insert((x, y)); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +#[cfg(test)] |
| 128 | +mod tests { |
| 129 | + use super::*; |
| 130 | + use crate::util::io_helpers::read_input_from_resources; |
| 131 | + |
| 132 | + #[test] |
| 133 | + fn test_result_16_01() { |
| 134 | + let (year, day) = (2016, 1); |
| 135 | + let expected_result = (253, 126); |
| 136 | + |
| 137 | + let raw_input = read_input_from_resources(year, day, false); |
| 138 | + assert_eq!(solve_16_01(raw_input), expected_result); |
| 139 | + } |
| 140 | +} |
0 commit comments