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