1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
/*
--- Day 3: Gear Ratios ---
You and the Elf eventually reach a gondola lift station; he says the gondola lift will take you up to the water source,
but this is as far as he can bring you. You go inside.
It doesn't take long to find the gondolas, but there seems to be a problem: they're not moving.
"Aaah!"
You turn around to see a slightly-greasy Elf with a wrench and a look of surprise. "Sorry, I wasn't expecting anyone!
The gondola lift isn't working right now; it'll still be a while before I can fix it." You offer to help.
The engineer explains that an engine part seems to be missing from the engine, but nobody can figure out which one. If
you can add up all the part numbers in the engine schematic, it should be easy to work out which part is missing.
The engine schematic (your puzzle input) consists of a visual representation of the engine. There are lots of numbers
and symbols you don't really understand, but apparently any number adjacent to a symbol, even diagonally, is a
"part number" and should be included in your sum. (Periods (.) do not count as a symbol.)
Here is an example engine schematic:
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..
In this schematic, two numbers are not part numbers because they are not adjacent to a symbol: 114 (top right) and 58
(middle right). Every other number is adjacent to a symbol and so is a part number; their sum is 4361.
Of course, the actual engine schematic is much larger. What is the sum of all of the part numbers in the engine schematic?
--- Day 3: Gear Ratios ---
You and the Elf eventually reach a gondola lift station; he says the gondola lift will take you up to the water source, but this is as far as he can bring you. You go inside.
It doesn't take long to find the gondolas, but there seems to be a problem: they're not moving.
"Aaah!"
You turn around to see a slightly-greasy Elf with a wrench and a look of surprise. "Sorry, I wasn't expecting anyone! The gondola lift isn't working right now; it'll still be a while before I can fix it." You offer to help.
The engineer explains that an engine part seems to be missing from the engine, but nobody can figure out which one. If you can add up all the part numbers in the engine schematic, it should be easy to work out which part is missing.
The engine schematic (your puzzle input) consists of a visual representation of the engine. There are lots of numbers and symbols you don't really understand, but apparently any number adjacent to a symbol, even diagonally, is a "part number" and should be included in your sum. (Periods (.) do not count as a symbol.)
Here is an example engine schematic:
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..
In this schematic, two numbers are not part numbers because they are not adjacent to a symbol: 114 (top right) and 58 (middle right). Every other number is adjacent to a symbol and so is a part number; their sum is 4361.
Of course, the actual engine schematic is much larger. What is the sum of all of the part numbers in the engine schematic?
--- Part Two ---
The engineer finds the missing part and installs it in the engine! As the engine springs to life, you jump in the
closest gondola, finally ready to ascend to the water source.
You don't seem to be going very fast, though. Maybe something is still wrong? Fortunately, the gondola has a phone
labeled "help", so you pick it up and the engineer answers.
Before you can explain the situation, she suggests that you look out the window. There stands the engineer, holding a
phone in one hand and waving with the other. You're going so slowly that you haven't even left the station. You exit
the gondola.
The missing part wasn't the only issue - one of the gears in the engine is wrong. A gear is any * symbol that is
adjacent to exactly two part numbers. Its gear ratio is the result of multiplying those two numbers together.
This time, you need to find the gear ratio of every gear and add them all up so that the engineer can figure out
which gear needs to be replaced.
Consider the same engine schematic again:
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..
In this schematic, there are two gears. The first is in the top left; it has part numbers 467 and 35, so its gear ratio
is 16345. The second gear is in the lower right; its gear ratio is 451490. (The * adjacent to 617 is not a gear because
it is only adjacent to one part number.) Adding up all of the gear ratios produces 467835.
What is the sum of all of the gear ratios in your engine schematic?
*/
use std::fs;
fn main() {
let input = fs::read("input").unwrap();
let w = input.iter().position(|&c| c == u8::try_from('\n').unwrap()).unwrap();
// let h = input.iter().filter(|&&c| c == u8::try_from('\n').unwrap()).count();
assert_eq!(w, 140);
let arr: Vec<char> = input
.iter()
.filter_map(|&c| {
return if c != u8::try_from('\n').unwrap() {
Some(c as char)
} else {
None
};
})
.collect();
let symbols: Vec<i32> = arr
.iter()
.enumerate()
.filter_map(|(i, &c)| {
return if !c.is_ascii_digit() && c != '.' {
Some(i as i32)
} else {
None
};
})
.collect();
// and now for the horribly inefficient part
let mut part_numbers: Vec<u32> = Vec::new();
let mut i= 0;
while i < arr.len() {
// look for a digit
if !arr.get(i).unwrap().is_ascii_digit() {
i += 1;
continue;
}
// gobble digits until EOL or nondigits
let mut j= 0;
let mut n = 0;
while i + j + 1 % w != 0 && i + j < arr.len()
&& arr.get(i + j).unwrap().is_ascii_digit()
{
n *= 10;
n += arr.get(i + j).unwrap().to_digit(10).unwrap();
j += 1;
}
// check for symbol adjacency
let mut adj = false;
let left = i as i32 - 1;
let right = i as i32 + j as i32;
// current row
if symbols.contains(&left) { adj = true }
if symbols.contains(&right) { adj = true }
// previous row
if left > w as i32 {
for k in left - w as i32 .. right + 1 - w as i32 {
if symbols.contains(&k) { adj = true }
}
}
// next row
if (right + w as i32) < arr.len() as i32 {
for k in left + w as i32 .. right + 1 + w as i32 {
if symbols.contains(&k) { adj = true }
}
}
// wrap up
i += j;
if adj {
part_numbers.push(n);
}
}
let sum:u32 = part_numbers.iter().sum();
println!("Part one answer: {sum}");
// PART TWO
let mut n_gears = 0;
// in which we realize that searching the other direction was better
let sumproduct: u32 = arr.iter().enumerate()
.filter_map(|(i, &c)| {
return if gear(i, &arr, w).is_some() {
n_gears += 1;
gear_ratio(i, &arr, w)
} else {
None
};
})
.reduce(|acc, e| acc + e)
.unwrap();
println!("{n_gears} gears found");
println!("Part two answer: {sumproduct}");
}
fn gear(loc: usize, arr: &Vec<char>, width: usize) -> Option<i32> {
if arr.get(loc).unwrap() != &'*' { return None }
// valid gears come in an enumerable number of flavors:
// . is a non-digit character
// 1 is a digit
// ??? can be ..1, .11, 111, 11., 1.., .1., but never 1.1
let bitmasks = [
// 1.1.*....
0b101000000,
// ...1*1...
0b000101000,
// ....*.1.1
0b000000101,
// ???1*....
0b100100000,
0b010100000,
0b001100000,
0b110100000,
0b111100000,
0b011100000,
// ???.*1...
0b100001000,
0b010001000,
0b001001000,
0b110001000,
0b011001000,
0b111001000,
// ...1*.???
0b000100100,
0b000100010,
0b000100001,
0b000100110,
0b000100011,
0b000100111,
// ....*1???
0b000001100,
0b000001010,
0b000001001,
0b000001110,
0b000001011,
0b000001111,
// ???.*.???
0b100000100,
0b010000100,
0b001000100,
0b110000100,
0b011000100,
0b111000100,
0b100000010,
0b010000010,
0b001000010,
0b110000010,
0b011000010,
0b111000010,
0b100000001,
0b010000001,
0b001000001,
0b110000001,
0b011000001,
0b111000001,
0b100000110,
0b010000110,
0b001000110,
0b110000110,
0b011000110,
0b111000110,
0b100000011,
0b010000011,
0b001000011,
0b110000011,
0b011000011,
0b111000011,
0b100000111,
0b010000111,
0b001000111,
0b110000111,
0b011000111,
0b111000111,
];
let mut neighborhood:i32 = 0;
if arr.get(loc - width - 1).ok_or('.').unwrap().is_ascii_digit() {
neighborhood |= 0b100000000;
}
if arr.get(loc - width ).ok_or('.').unwrap().is_ascii_digit() {
neighborhood |= 0b010000000;
}
if arr.get(loc - width + 1).ok_or('.').unwrap().is_ascii_digit() {
neighborhood |= 0b001000000;
}
if arr.get(loc - 1).ok_or('.').unwrap().is_ascii_digit() {
neighborhood |= 0b000100000;
}
if arr.get(loc + 1).ok_or('.').unwrap().is_ascii_digit() {
neighborhood |= 0b000001000;
}
if arr.get(loc + width - 1).ok_or('.').unwrap().is_ascii_digit() {
neighborhood |= 0b000000100;
}
if arr.get(loc + width ).ok_or('.').unwrap().is_ascii_digit() {
neighborhood |= 0b000000010;
}
if arr.get(loc + width + 1).ok_or('.').unwrap().is_ascii_digit() {
neighborhood |= 0b000000001;
}
bitmasks.contains(&neighborhood).then(|| neighborhood)
}
fn read_number(loc: usize, arr: &Vec<char>, width: usize) -> Option<u32> {
if !arr.get(loc).unwrap().is_ascii_digit() { return None }
// scan left to start of number
let mut i = loc;
while i % width != 0 && arr.get(i-1).unwrap().is_ascii_digit() {
i -= 1;
}
// read right to complete the number
let mut n = 0;
while i+1 % width != 0 && arr.get(i).unwrap().is_ascii_digit() {
n *= 10;
n += arr.get(i).unwrap().to_digit(10).unwrap();
i += 1;
}
assert!(n < 1000);
(n != 0).then(|| n)
}
fn gear_ratio(loc: usize, arr: &Vec<char>, width: usize) -> Option<u32> {
let neighborhood = gear(loc, arr, width)?;
return if neighborhood & 0b111000000 == 0b101000000 {
// special case: two numbers on top
let x = read_number(loc - width - 1, arr, width).unwrap();
let y = read_number(loc - width + 1, arr, width).unwrap();
// println!("{x} {y}");
Some(x * y)
} else if neighborhood & 0b000000111 == 0b000000101 {
// special case: two numbers on bottom
let x = read_number(loc + width - 1, arr, width).unwrap();
let y = read_number(loc + width + 1, arr, width).unwrap();
// println!("{x} {y}");
Some(x * y)
} else {
let top =
read_number(loc - width - 1, arr, width)
.or(read_number(loc - width , arr, width))
.or(read_number(loc - width + 1, arr, width))
.or(Some(1))
.unwrap();
let left = read_number(loc - 1, arr, width).or(Some(1)).unwrap();
let right = read_number(loc + 1, arr, width).or(Some(1)).unwrap();
let bottom =
read_number(loc + width - 1, arr, width)
.or(read_number(loc + width , arr, width))
.or(read_number(loc + width + 1, arr, width))
.or(Some(1))
.unwrap();
// println!("{top} {left} {right} {bottom}");
Some(top * left * right * bottom)
}
}
|