-
Notifications
You must be signed in to change notification settings - Fork 0
/
day13.ts
58 lines (45 loc) · 1.73 KB
/
day13.ts
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
import * as fs from 'fs';
const input = fs.readFileSync('day13.txt', { encoding: 'utf8' })
.split('\r\n\r\n')
.map(x => x.split('\r\n'));
interface Reflection { type: 'x' | 'y', index: number };
const reflectionValue = (reflection: Reflection) => reflection.type === 'x' ? reflection.index + 1 : (reflection.index + 1) * 100;
function getReflection(map: string[], i: number, type: 'x' | 'y'): Reflection | undefined {
let step = 0;
while (true) {
if (i - step < 0) break;
if (i + 1 + step >= map.length) break;
if (map[i - step] !== map[i + 1 + step]) return undefined;
step++;
}
return step > 0 ? { type: type, index: i } : undefined;
}
function* genMapsWithSmudgeToggled(map: string[]): Iterable<string[]> {
for (let row = 0; row < map.length; row++) {
for (let col = 0; col < map[0].length; col++) {
const copy = map.slice().map(x => x.split(''))
copy[row][col] = copy[row][col] === '.' ? '#' : '.';
yield copy.map(x => x.join(''));
}
}
}
function getReflections(map: string[]): Reflection[] {
const rows = map;
const cols = Array.from({ length: map[0].length }).map((_, i) => map.map(y => y[i]).join(''));
return [
...rows.map((_, i) => getReflection(rows, i, 'y')),
...cols.map((_, i) => getReflection(cols, i, 'x'))
].filter(x => x !== undefined);
}
let part1 = 0;
let part2 = 0;
for (const map of input) {
const reflection = getReflections(map)[0];
const smudgeReflection = [...genMapsWithSmudgeToggled(map)]
.flatMap(x => getReflections(x))
.find(x => x.index !== reflection.index || x.type !== reflection.type);
part1 += reflectionValue(reflection);
part2 += reflectionValue(smudgeReflection);
}
console.log('Part 1', part1);
console.log('Part 1', part2);