-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13.ts
119 lines (101 loc) · 2.54 KB
/
13.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
import Task, { TaskPartSolution } from "../utils/task.js";
const checkMirror = (row: string, n: number): boolean => {
for (let i = 0; i < n; i++) {
const l = row[n - i - 1];
const r = row[n + i];
if (l == null || r == null) return true;
if (l !== r) return false;
}
return true;
};
const rowAxis = (row: string, ignore?: number): number[] => {
const res = [];
for (let i = 1; i < row.length; i++) {
if (i !== ignore && checkMirror(row, i)) {
res.push(i);
}
}
return res;
};
const groupAxis = (g: string[], ignore?: number): number | undefined => {
return g.map((r) => rowAxis(r, ignore)).reduce((acc, s) => acc.filter((n) => s.includes(n)))[0];
};
const transpose = (g: string[]): string[] => {
let cols: string[] = new Array(g[0].length).fill("");
return cols.map((c, i) => g.map((r) => r[i]).join(""));
};
const groupNumber = (g: string, ignore?: number): number | undefined => {
const rows = g.split("\n");
const r = groupAxis(rows, ignore && ignore < 100 ? ignore : undefined);
if (r !== undefined) return r;
const c = groupAxis(transpose(rows), ignore && ignore >= 100 ? ignore / 100 : undefined);
if (c !== undefined) return c * 100;
return undefined;
};
const part1: TaskPartSolution = (input) => {
const groups = input.split("\n\n");
return groups.map((g) => groupNumber(g)!).reduce((acc, n) => acc + n);
};
const groupNumberSmudge = (g: string): number => {
const rows = g.split("\n");
const baseNum = groupNumber(g);
for (let y = 0; y < rows.length; y++) {
for (let x = 0; x < rows[0].length; x++) {
const newRows = rows
.map((r, ry) =>
r
.split("")
.map((c, cx) => (ry === y && cx === x ? (c === "#" ? "." : "#") : c))
.join("")
)
.join("\n");
const newNum = groupNumber(newRows, baseNum);
if (newNum !== undefined) {
return newNum;
}
}
}
return NaN;
};
const part2: TaskPartSolution = (input) => {
const groups = input.split("\n\n");
return groups.map(groupNumberSmudge).reduce((acc, n) => acc + n);
};
const task = new Task(2023, 13, part1, part2, {
part1: {
input: `#.##..##.
..#.##.#.
##......#
##......#
..#.##.#.
..##..##.
#.#.##.#.
#...##..#
#....#..#
..##..###
#####.##.
#####.##.
..##..###
#....#..#`,
result: "405",
},
part2: {
input: `#.##..##.
..#.##.#.
##......#
##......#
..#.##.#.
..##..##.
#.#.##.#.
#...##..#
#....#..#
..##..###
#####.##.
#####.##.
..##..###
#....#..#`,
result: "400",
},
});
export default task;
// start 6:00; end ~7:15