1use std::array::from_fn;
45
46use crate::util::parse::*;
47
48pub struct Tile {
49 id: u64,
50 top: [usize; 8],
51 left: [usize; 8],
52 bottom: [usize; 8],
53 right: [usize; 8],
54 pixels: [[u8; 10]; 10],
55}
56
57impl Tile {
58 const COEFFICIENTS: [[i32; 6]; 8] = [
64 [1, 0, 1, 0, 1, 1],
65 [-1, 0, 8, 0, 1, 1],
66 [1, 0, 1, 0, -1, 8],
67 [-1, 0, 8, 0, -1, 8],
68 [0, 1, 1, -1, 0, 8],
69 [0, 1, 1, 1, 0, 1],
70 [0, -1, 8, -1, 0, 8],
71 [0, -1, 8, 1, 0, 1],
72 ];
73
74 fn new(chunk: &[&str]) -> Self {
75 let id = chunk[0][5..9].unsigned();
76
77 let pixels: [[_; 10]; 10] = from_fn(|i| chunk[i + 1].as_bytes().try_into().unwrap());
78
79 let binary = |row: usize, col: usize| usize::from(pixels[row][col] & 1);
82 let (t, l, b, r) = (0..10).fold((0, 0, 0, 0), |(t, l, b, r), i| {
83 (
84 (t << 1) | binary(0, i),
85 (l << 1) | binary(i, 0),
86 (b << 1) | binary(9, i),
87 (r << 1) | binary(i, 9),
88 )
89 });
90
91 let [rt, rl, rb, rr] = [t, l, b, r].map(|edge: usize| edge.reverse_bits() >> 54);
92
93 let top = [t, rt, b, rb, rl, l, rr, r];
96 let left = [l, r, rl, rr, b, t, rb, rt];
97 let bottom = [b, rb, t, rt, rr, r, rl, l];
98 let right = [r, l, rr, rl, t, b, rt, rb];
99
100 Self { id, top, left, bottom, right, pixels }
101 }
102
103 fn transform(&self, image: &mut [u128], permutation: usize) {
105 let [a, b, c, d, e, f] = Self::COEFFICIENTS[permutation];
106
107 for (row, line) in (0..8).zip(image) {
108 let acc = (0..8).fold(0, |acc, col| {
109 let x = a * col + b * row + c;
110 let y = d * col + e * row + f;
111 let b = self.pixels[y as usize][x as usize];
112 (acc << 1) | (b & 1)
113 });
114
115 *line = (*line << 8) | u128::from(acc);
116 }
117 }
118}
119
120pub fn parse(input: &str) -> Vec<Tile> {
121 let lines: Vec<_> = input.lines().collect();
122 lines.chunks(12).map(Tile::new).collect()
123}
124
125pub fn part1(input: &[Tile]) -> u64 {
126 let mut freq = [0; 1024];
127
128 for edge in input.iter().flat_map(|t| t.top) {
129 freq[edge] += 1;
130 }
131
132 input
134 .iter()
135 .filter(|t| freq[t.top[0]] + freq[t.left[0]] + freq[t.bottom[0]] + freq[t.right[0]] == 6)
136 .map(|t| t.id)
137 .product()
138}
139
140pub fn part2(input: &[Tile]) -> u32 {
141 let mut edge_to_tile = [[0; 2]; 1024];
144 let mut freq = [0; 1024];
145 let mut placed = [false; 1024];
146
147 for (i, tile) in input.iter().enumerate() {
148 for edge in tile.top {
149 edge_to_tile[edge][freq[edge]] = i;
150 freq[edge] += 1;
151 }
152 }
153
154 let mut next_top = input
155 .iter()
156 .flat_map(|tile| tile.top.iter().zip(&tile.left))
157 .find_map(|(&top, &left)| (freq[top] == 1 && freq[left] == 1).then_some(top))
158 .unwrap();
159 freq[next_top] += 1;
160
161 let mut find_matching_tile = |edge| {
162 let [first, second] = edge_to_tile[edge];
163 let next = if placed[first] { second } else { first };
164 placed[next] = true;
165 &input[next]
166 };
167
168 let mut image = [0; 96];
170 let mut index = 0;
171
172 while freq[next_top] == 2 {
173 let tile = find_matching_tile(next_top);
174 let permutation = tile.top.iter().position(|&top| top == next_top).unwrap();
175 tile.transform(&mut image[index..], permutation);
176 next_top = tile.bottom[permutation];
177
178 let mut next_left = tile.right[permutation];
179
180 while freq[next_left] == 2 {
181 let tile = find_matching_tile(next_left);
182 let permutation = tile.left.iter().position(|&left| left == next_left).unwrap();
183 tile.transform(&mut image[index..], permutation);
184 next_left = tile.right[permutation];
185 }
186
187 index += 8;
188 }
189
190 let sea = image.iter().map(|n| n.count_ones()).sum();
192 let find = |monster: &mut [u128], width: usize, height: usize| {
193 let mut rough = sea;
194
195 for _ in 0..=(96 - width) {
196 for window in image.windows(height) {
197 if monster.iter().zip(window).all(|(&mask, &row)| mask & row == mask) {
198 rough -= 15;
199 }
200 }
201 monster.iter_mut().for_each(|n| *n <<= 1);
202 }
203
204 (rough < sea).then_some(rough)
205 };
206
207 let mut monsters = [
210 [0b00000000000000000010, 0b10000110000110000111, 0b01001001001001001000],
211 [0b01001001001001001000, 0b10000110000110000111, 0b00000000000000000010],
212 [0b01000000000000000000, 0b11100001100001100001, 0b00010010010010010010],
213 [0b00010010010010010010, 0b11100001100001100001, 0b01000000000000000000],
214 ];
215
216 if let Some(rough) = monsters.iter_mut().find_map(|monster| find(monster, 20, 3)) {
217 return rough;
218 }
219
220 let mut monsters = [
222 [2, 4, 0, 0, 4, 2, 2, 4, 0, 0, 4, 2, 2, 4, 0, 0, 4, 2, 3, 2],
223 [2, 3, 2, 4, 0, 0, 4, 2, 2, 4, 0, 0, 4, 2, 2, 4, 0, 0, 4, 2],
224 [2, 1, 0, 0, 1, 2, 2, 1, 0, 0, 1, 2, 2, 1, 0, 0, 1, 2, 6, 2],
225 [2, 6, 2, 1, 0, 0, 1, 2, 2, 1, 0, 0, 1, 2, 2, 1, 0, 0, 1, 2],
226 ];
227
228 monsters.iter_mut().find_map(|monster| find(monster, 3, 20)).unwrap()
229}