|
| 1 | +use std::collections::{HashSet, VecDeque}; |
| 2 | + |
| 3 | +use aoc_lib::{ |
| 4 | + answer::Answer, |
| 5 | + directions::{Advance, Cardinal, Direction}, |
| 6 | + matrix::Matrix, |
| 7 | + solution::Solution, |
| 8 | + vec2::Vec2, |
| 9 | +}; |
| 10 | + |
| 11 | +pub struct Day12; |
| 12 | + |
| 13 | +impl Solution for Day12 { |
| 14 | + fn part_a(&self, input: &[String]) -> Answer { |
| 15 | + let mut map = Map::from_input(input); |
| 16 | + map.fence_cost(false).into() |
| 17 | + } |
| 18 | + |
| 19 | + fn part_b(&self, input: &[String]) -> Answer { |
| 20 | + let mut map = Map::from_input(input); |
| 21 | + map.fence_cost(true).into() |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +struct Map { |
| 26 | + map: Matrix<char>, |
| 27 | + regions: Option<Vec<Region>>, |
| 28 | +} |
| 29 | + |
| 30 | +struct Region { |
| 31 | + area: HashSet<Vec2<usize>>, |
| 32 | + perimeter: usize, |
| 33 | + corners: usize, |
| 34 | +} |
| 35 | + |
| 36 | +impl Region { |
| 37 | + fn new() -> Self { |
| 38 | + Self { |
| 39 | + area: HashSet::new(), |
| 40 | + perimeter: 0, |
| 41 | + corners: 0, |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl Map { |
| 47 | + fn from_input(input: &[String]) -> Self { |
| 48 | + Self { |
| 49 | + map: Matrix::from_chars(input), |
| 50 | + regions: None, |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + fn compute_regions(&mut self) { |
| 55 | + let mut all = vec![]; |
| 56 | + let mut visited = HashSet::new(); |
| 57 | + for y in 0..self.map.rows { |
| 58 | + for x in 0..self.map.cols { |
| 59 | + let pos = Vec2::new(x, y); |
| 60 | + if !visited.contains(&pos) { |
| 61 | + let region = self.flood(pos, &mut visited); |
| 62 | + all.push(region); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + self.regions = Some(all); |
| 67 | + } |
| 68 | + |
| 69 | + fn flood(&self, initial: Vec2<usize>, visited: &mut HashSet<Vec2<usize>>) -> Region { |
| 70 | + let mut region = Region::new(); |
| 71 | + let mut queue: VecDeque<Vec2<usize>> = VecDeque::new(); |
| 72 | + queue.push_front(initial); |
| 73 | + visited.insert(initial); |
| 74 | + |
| 75 | + while let Some(pos) = queue.pop_front() { |
| 76 | + region.area.insert(pos); |
| 77 | + |
| 78 | + for direction in Cardinal::all_clockwise() { |
| 79 | + let next: Vec2<isize> = direction.advance(pos.into()); |
| 80 | + |
| 81 | + let Some(&next_plant_type) = self.map.get(&next) else { |
| 82 | + region.perimeter += 1; |
| 83 | + continue; |
| 84 | + }; |
| 85 | + |
| 86 | + if next_plant_type != self.map[pos] { |
| 87 | + region.perimeter += 1; |
| 88 | + continue; |
| 89 | + } |
| 90 | + |
| 91 | + if let Ok(n) = Vec2::<usize>::try_from(next) { |
| 92 | + if !visited.contains(&n) { |
| 93 | + visited.insert(n); |
| 94 | + queue.push_back(n); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + region |
| 100 | + } |
| 101 | + |
| 102 | + // The corners can be counted only after all the connected components of the graph |
| 103 | + // has been fully computed |
| 104 | + fn count_corners(&mut self) { |
| 105 | + for region in self.regions.as_mut().unwrap() { |
| 106 | + let area = region |
| 107 | + .area |
| 108 | + .iter() |
| 109 | + .map(|&p| Vec2::<isize>::from(p)) |
| 110 | + .collect::<HashSet<_>>(); |
| 111 | + for tile in ®ion.area { |
| 112 | + let sized_tile = Vec2::<isize>::from(tile); |
| 113 | + // Check for corners going inside exterior (Concave from the point of view of the |
| 114 | + // tile) |
| 115 | + // Exemple : You don't have front and right, you are at a corner. |
| 116 | + for direction in Cardinal::all_clockwise() { |
| 117 | + if !area.contains(&direction.advance(sized_tile)) |
| 118 | + && !area.contains(&direction.turn_right().advance(sized_tile)) |
| 119 | + { |
| 120 | + region.corners += 1; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + // Check for corners going inside a hole (Convex from the point of view of the tile) |
| 125 | + // A convex corner is when you have both neighbors but missing the diagonal |
| 126 | + // Exemple : You have front and right but don't have front right, you are at a |
| 127 | + // corner |
| 128 | + for direction in Cardinal::all_clockwise() { |
| 129 | + let diagonal = direction |
| 130 | + .turn_right() |
| 131 | + .advance(direction.advance(sized_tile)); |
| 132 | + if area.contains(&direction.advance(sized_tile)) |
| 133 | + && area.contains(&direction.turn_right().advance(sized_tile)) |
| 134 | + && !area.contains(&diagonal) |
| 135 | + { |
| 136 | + region.corners += 1; |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + fn fence_cost(&mut self, discount: bool) -> usize { |
| 144 | + self.compute_regions(); |
| 145 | + if discount { |
| 146 | + self.count_corners(); |
| 147 | + } |
| 148 | + let mut total = 0; |
| 149 | + for region in self.regions.as_ref().unwrap() { |
| 150 | + let mul = if discount { |
| 151 | + region.corners |
| 152 | + } else { |
| 153 | + region.perimeter |
| 154 | + }; |
| 155 | + total += region.area.len() * mul; |
| 156 | + } |
| 157 | + total |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +#[cfg(test)] |
| 162 | +mod test { |
| 163 | + use aoc_lib::{answer::Answer, input, solution::Solution}; |
| 164 | + |
| 165 | + use super::Day12; |
| 166 | + |
| 167 | + #[test] |
| 168 | + fn test_a() { |
| 169 | + let input = |
| 170 | + input::read_file(&format!("{}day_12_test.txt", crate::FILES_PREFIX_TEST)).unwrap(); |
| 171 | + let answer = Day12.part_a(&input); |
| 172 | + assert_eq!(<i32 as Into<Answer>>::into(1930), answer); |
| 173 | + } |
| 174 | + |
| 175 | + #[test] |
| 176 | + fn test_b() { |
| 177 | + let input = |
| 178 | + input::read_file(&format!("{}day_12_test.txt", crate::FILES_PREFIX_TEST)).unwrap(); |
| 179 | + let answer = Day12.part_b(&input); |
| 180 | + assert_eq!(<i32 as Into<Answer>>::into(1206), answer); |
| 181 | + } |
| 182 | +} |
0 commit comments