-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday13.ts
177 lines (148 loc) · 3.69 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
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
const test_input = `#.##..##.
..#.##.#.
##......#
##......#
..#.##.#.
..##..##.
#.#.##.#.
#...##..#
#....#..#
..##..###
#####.##.
#####.##.
..##..###
#....#..#`.split('\n');
const puzzle_input = `<get from aoc website>`.split('\n');
function log(u: unknown) {
console.log(JSON.stringify(u, undefined, 2));
}
function rotateMatrixRight(matrix: string[][]): string[][] {
return matrix[0].map((_, index) =>
matrix.map((row) => row[row.length - 1 - index]),
);
}
function makeMirrors(input: string[]) {
const mirrors = input.reduce(
(a: string[][], c) => {
if (c.length === 0) {
a.push([]);
} else {
a[a.length - 1].push(c);
}
return a;
},
[[]],
);
return mirrors;
}
function part1(input: string[]) {
const mirrors = makeMirrors(input);
const reflectionScore = mirrors.reduce((a, c) => {
let rc: number[] = [];
for (let i = 0; i < c.length - 1; i++) {
if (c[i] === c[i + 1]) {
rc.push(i);
}
}
const rows = rc.reduce((a: number | null, entry) => {
let i = entry;
let j = i + 1;
while (i > -1 && j < c.length) {
if (c[i] !== c[j]) {
return a;
}
i--;
j++;
}
return (entry + 1) * 100;
}, null);
const cc: number[] = [];
const rotated = rotateMatrixRight(c.map((r) => Array.from(r))).map((r) =>
r.join(''),
);
for (let i = 0; i < rotated.length - 1; i++) {
if (rotated[i] === rotated[i + 1]) {
cc.push(i);
}
}
const cols = cc.reduce((a: number | null, entry) => {
let i = entry;
let j = i + 1;
while (i > -1 && j < rotated.length) {
if (rotated[i] !== rotated[j]) {
return a;
}
i--;
j++;
}
return rotated.length - entry - 1;
}, null);
return a + (cols ? cols : 0) + (rows ? rows : 0);
}, 0);
log(reflectionScore);
}
function part2(input: string[]) {
const mirrors = makeMirrors(input);
const diffCount = (l: string, r: string) => {
let smudgeCnt = 0;
for (let p = 0; p < l.length; p++) {
if (l[p] !== r[p]) {
smudgeCnt += 1;
}
}
return smudgeCnt;
};
const reflectionScore = mirrors.reduce((a, mir) => {
let rc: number[] = [];
for (let i = 0; i < mir.length - 1; i++) {
const smudgeCnt = diffCount(mir[i], mir[i + 1]);
if (smudgeCnt <= 1) {
rc.push(i);
}
}
const rows = rc.reduce((a: number | null, entry) => {
let i = entry;
let j = i + 1;
let totalSmudge = 0;
while (i > -1 && j < mir.length) {
const smudgeCnt = diffCount(mir[i], mir[j]);
totalSmudge += smudgeCnt;
if (totalSmudge > 1) {
return a;
}
i--;
j++;
}
return totalSmudge === 1 ? (entry + 1) * 100 : a;
}, null);
const cc: number[] = [];
const rotated = rotateMatrixRight(mir.map((r) => Array.from(r))).map((r) =>
r.join(''),
);
for (let i = 0; i < rotated.length - 1; i++) {
const smudgeCnt = diffCount(rotated[i], rotated[i + 1]);
if (smudgeCnt <= 1) {
cc.push(i);
}
}
const cols = cc.reduce((a: number | null, entry) => {
let i = entry;
let j = i + 1;
let totalSmudge = 0;
while (i > -1 && j < rotated.length) {
const smudgeCnt = diffCount(rotated[i], rotated[j]);
totalSmudge += smudgeCnt;
if (totalSmudge > 1) {
return a;
}
i--;
j++;
}
return totalSmudge === 1 ? rotated.length - entry - 1 : a;
}, null);
return a + (cols ? cols : 0) + (rows ? rows : 0);
}, 0);
log(reflectionScore);
}
// part1(test_input);
part2(puzzle_input);