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 (t, l, b, r) = (0..10).fold((0, 0, 0, 0), |(t, l, b, r), i| {
82 (
83 (t << 1) | binary(0, i),
84 (l << 1) | binary(i, 0),
85 (b << 1) | binary(9, i),
86 (r << 1) | binary(i, 9),
87 )
88 });
89
90 let reverse = |edge: usize| edge.reverse_bits() >> 54;
91 let rt = reverse(t);
92 let rl = reverse(l);
93 let rb = reverse(b);
94 let rr = reverse(r);
95
96 let top = [t, rt, b, rb, rl, l, rr, r];
99 let left = [l, r, rl, rr, b, t, rb, rt];
100 let bottom = [b, rb, t, rt, rr, r, rl, l];
101 let right = [r, l, rr, rl, t, b, rt, rb];
102
103 Tile { id, top, left, bottom, right, pixels }
104 }
105
106 fn transform(&self, image: &mut [u128], permutation: usize) {
108 let [a, b, c, d, e, f] = Self::COEFFICIENTS[permutation];
109
110 for row in 0..8 {
111 let mut acc = 0;
112
113 for col in 0..8 {
114 let x = a * col + b * row + c;
115 let y = d * col + e * row + f;
116 let b = self.pixels[y as usize][x as usize];
117 acc = (acc << 1) | (b & 1);
118 }
119
120 image[row as usize] = (image[row as usize] << 8) | (acc as u128);
121 }
122 }
123}
124
125pub fn parse(input: &str) -> Vec<Tile> {
126 let lines: Vec<_> = input.lines().collect();
127 lines.chunks(12).map(Tile::from).collect()
128}
129
130pub fn part1(input: &[Tile]) -> u64 {
131 let mut freq = [0; 1024];
132 let mut result = 1;
133
134 for edge in input.iter().flat_map(|t| t.top) {
135 freq[edge] += 1;
136 }
137
138 for tile in input {
139 let total =
141 freq[tile.top[0]] + freq[tile.left[0]] + freq[tile.bottom[0]] + freq[tile.right[0]];
142 if total == 6 {
143 result *= tile.id;
144 }
145 }
146
147 result
148}
149
150pub fn part2(input: &[Tile]) -> u32 {
151 let mut edge_to_tile = [[0; 2]; 1024];
154 let mut freq = [0; 1024];
155 let mut placed = [false; 1024];
156
157 for (i, tile) in input.iter().enumerate() {
158 for edge in tile.top {
159 edge_to_tile[edge][freq[edge]] = i;
160 freq[edge] += 1;
161 }
162 }
163
164 let mut find_arbitrary_corner = || {
165 for tile in input {
166 for j in 0..8 {
167 if freq[tile.top[j]] == 1 && freq[tile.left[j]] == 1 {
168 freq[tile.top[j]] += 1;
169 return tile.top[j];
170 }
171 }
172 }
173 unreachable!()
174 };
175 let mut find_matching_tile = |edge: usize| {
176 let [first, second] = edge_to_tile[edge];
177 let next = if placed[first] { second } else { first };
178 placed[next] = true;
179 &input[next]
180 };
181
182 let mut next_top = find_arbitrary_corner();
184 let mut image = [0; 96];
185 let mut index = 0;
186
187 while freq[next_top] == 2 {
188 let tile = find_matching_tile(next_top);
189 let permutation = (0..8).position(|i| tile.top[i] == next_top).unwrap();
190 tile.transform(&mut image[index..], permutation);
191 next_top = tile.bottom[permutation];
192
193 let mut next_left = tile.right[permutation];
194
195 while freq[next_left] == 2 {
196 let tile = find_matching_tile(next_left);
197 let permutation = (0..8).position(|i| tile.left[i] == next_left).unwrap();
198 tile.transform(&mut image[index..], permutation);
199 next_left = tile.right[permutation];
200 }
201
202 index += 8;
203 }
204
205 let sea: u32 = image.iter().map(|n| n.count_ones()).sum();
207 let find = |monster: &mut [u128], width: usize, height: usize| {
208 let mut rough = sea;
209
210 for _ in 0..(96 - width + 1) {
211 for window in image.windows(height) {
212 if monster.iter().enumerate().all(|(i, &n)| n & window[i] == n) {
213 rough -= 15;
214 }
215 }
216 monster.iter_mut().for_each(|n| *n <<= 1);
217 }
218
219 (rough < sea).then_some(rough)
220 };
221
222 let mut monsters = [
225 [0b00000000000000000010, 0b10000110000110000111, 0b01001001001001001000],
226 [0b01001001001001001000, 0b10000110000110000111, 0b00000000000000000010],
227 [0b01000000000000000000, 0b11100001100001100001, 0b00010010010010010010],
228 [0b00010010010010010010, 0b11100001100001100001, 0b01000000000000000000],
229 ];
230
231 for monster in &mut monsters {
232 if let Some(rough) = find(monster, 20, 3) {
233 return rough;
234 }
235 }
236
237 let mut monsters = [
239 [2, 4, 0, 0, 4, 2, 2, 4, 0, 0, 4, 2, 2, 4, 0, 0, 4, 2, 3, 2],
240 [2, 3, 2, 4, 0, 0, 4, 2, 2, 4, 0, 0, 4, 2, 2, 4, 0, 0, 4, 2],
241 [2, 1, 0, 0, 1, 2, 2, 1, 0, 0, 1, 2, 2, 1, 0, 0, 1, 2, 6, 2],
242 [2, 6, 2, 1, 0, 0, 1, 2, 2, 1, 0, 0, 1, 2, 2, 1, 0, 0, 1, 2],
243 ];
244
245 for monster in &mut monsters {
246 if let Some(rough) = find(monster, 3, 20) {
247 return rough;
248 }
249 }
250
251 unreachable!()
252}