|
| 1 | +advent_of_code::solution!(6); |
| 2 | + |
| 3 | +use advent_of_code::maneatingape::hash::*; |
| 4 | +use advent_of_code::maneatingape::point::*; |
| 5 | + |
| 6 | +type Direction = Point; |
| 7 | + |
| 8 | +struct MapSize { |
| 9 | + min_x: i32, |
| 10 | + max_x: i32, |
| 11 | + min_y: i32, |
| 12 | + max_y: i32, |
| 13 | +} |
| 14 | + |
| 15 | +struct DataType { |
| 16 | + position: Point, |
| 17 | + direction: Direction, |
| 18 | + obstructions: FastSet<Point>, |
| 19 | + map_size: MapSize, |
| 20 | +} |
| 21 | + |
| 22 | +impl MapSize { |
| 23 | + fn contains(&self, point: &Point) -> bool { |
| 24 | + point.x >= self.min_x |
| 25 | + && point.x <= self.max_x |
| 26 | + && point.y >= self.min_y |
| 27 | + && point.y <= self.max_y |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +fn parse_data(input: &str) -> DataType { |
| 32 | + let my_direction = UP; |
| 33 | + let mut my_position = Point::new(0, 0); |
| 34 | + let mut obstructions = FastSet::new(); |
| 35 | + |
| 36 | + let height = input.lines().count(); |
| 37 | + let width = input.split_once("\n").unwrap().0.len(); |
| 38 | + |
| 39 | + for (y, line) in input.lines().enumerate() { |
| 40 | + for (x, v) in line.bytes().enumerate() { |
| 41 | + match v { |
| 42 | + b'#' => { |
| 43 | + obstructions.insert(Point::new(x as i32, y as i32)); |
| 44 | + } |
| 45 | + b'^' => { |
| 46 | + my_position = Point::new(x as i32, y as i32); |
| 47 | + } |
| 48 | + _ => {} |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + DataType { |
| 54 | + position: my_position, |
| 55 | + direction: my_direction, |
| 56 | + obstructions, |
| 57 | + map_size: MapSize { |
| 58 | + min_x: 0, |
| 59 | + max_x: width as i32 - 1, |
| 60 | + min_y: 0, |
| 61 | + max_y: height as i32 - 1, |
| 62 | + }, |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +pub fn part_one(input: &str) -> Option<usize> { |
| 67 | + let data: DataType = parse_data(input); |
| 68 | + |
| 69 | + let mut my_position = data.position; |
| 70 | + let mut my_direction = data.direction; |
| 71 | + |
| 72 | + let mut visit = FastSet::new(); |
| 73 | + |
| 74 | + loop { |
| 75 | + visit.insert(my_position); |
| 76 | + |
| 77 | + let next_position = my_position + my_direction; |
| 78 | + if !data.map_size.contains(&next_position) { |
| 79 | + break; |
| 80 | + } |
| 81 | + |
| 82 | + if data.obstructions.contains(&next_position) { |
| 83 | + my_direction = my_direction.clockwise(); |
| 84 | + } else { |
| 85 | + my_position = next_position; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + let result = visit.len(); |
| 90 | + |
| 91 | + Some(result) |
| 92 | +} |
| 93 | + |
| 94 | +pub fn part_two(input: &str) -> Option<u32> { |
| 95 | + let data: DataType = parse_data(input); |
| 96 | + |
| 97 | + let mut result = 0; |
| 98 | + |
| 99 | + for y in data.map_size.min_y..=data.map_size.max_y { |
| 100 | + for x in data.map_size.min_x..=data.map_size.max_x { |
| 101 | + let new_obstruction = Point::new(x, y); |
| 102 | + |
| 103 | + if data.position == new_obstruction || data.obstructions.contains(&new_obstruction) { |
| 104 | + continue; |
| 105 | + } |
| 106 | + |
| 107 | + let mut visit: FastSet<(Point, Point)> = FastSet::new(); |
| 108 | + |
| 109 | + let mut my_position = data.position; |
| 110 | + let mut my_direction = data.direction; |
| 111 | + |
| 112 | + let stuck = loop { |
| 113 | + if !visit.insert((my_position, my_direction)) { |
| 114 | + break true; |
| 115 | + } |
| 116 | + |
| 117 | + let next_position = my_position + my_direction; |
| 118 | + if !data.map_size.contains(&next_position) { |
| 119 | + break false; |
| 120 | + } |
| 121 | + |
| 122 | + if new_obstruction == next_position || data.obstructions.contains(&next_position) { |
| 123 | + my_direction = my_direction.clockwise(); |
| 124 | + } else { |
| 125 | + my_position = next_position; |
| 126 | + } |
| 127 | + }; |
| 128 | + |
| 129 | + if stuck { |
| 130 | + result += 1; |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + Some(result) |
| 136 | +} |
| 137 | + |
| 138 | +#[cfg(test)] |
| 139 | +mod tests { |
| 140 | + use super::*; |
| 141 | + |
| 142 | + #[test] |
| 143 | + fn test_part_one() { |
| 144 | + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); |
| 145 | + assert_eq!(result, Some(41)); |
| 146 | + } |
| 147 | + |
| 148 | + #[test] |
| 149 | + fn test_part_two() { |
| 150 | + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); |
| 151 | + assert_eq!(result, Some(6)); |
| 152 | + } |
| 153 | +} |
0 commit comments