-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday11.ts
90 lines (73 loc) · 2.06 KB
/
day11.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
function log(u: unknown) {
console.log(JSON.stringify(u));
}
const test_input = `...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....`.split('\n');
const puzzle_input = `<get from aoc website>`.split('\n');
function part1(input: string[], age: number) {
const filledRow: boolean[] = new Array(input.length).fill(false);
const filledCol: boolean[] = new Array(input[0].length).fill(false);
const galaxies: [number, number][] = [];
for (let i = 0; i < input.length; i++) {
const r = input[i];
for (let j = 0; j < r.length; j++) {
const c = r[j];
if (c === '#') {
galaxies.push([i, j]);
filledCol[j] = true;
filledRow[i] = true;
}
}
}
const pairs: [[number, number], [number, number]][] = [];
for (let i = 0; i < galaxies.length; i++) {
for (let j = i + 1; j < galaxies.length; j++) {
pairs.push([galaxies[i], galaxies[j]]);
}
}
const emptyRow = filledRow
.map((v, i) => (v ? undefined : i))
.filter((v) => v != null);
const emptyCol = filledCol
.map((v, i) => (v ? undefined : i))
.filter((v) => v != null);
log(emptyCol);
log(emptyRow);
const distances = pairs.map((p) => {
const [f, s] = p;
const [x1, y1] = f;
const [x2, y2] = s;
const t = Math.min(x1, x2);
const b = Math.max(x1, x2);
const dx =
emptyRow.reduce((a, x) => {
if (x != null && t < x && x < b) {
return a == null ? 0 : a + age;
}
return a;
}, 0) ?? 0;
const l = Math.min(y1, y2);
const r = Math.max(y1, y2);
const dy =
emptyCol.reduce((a, y) => {
if (y != null && l < y && y < r) {
return a == null ? 0 : a + age;
}
return a;
}, 0) ?? 0;
const dist = Math.abs(x1 - x2) + Math.abs(y1 - y2) + dx + dy;
// log(`${JSON.stringify(p)}>>${Math.abs(x1 - x2)}+ ${Math.abs(y1 - y2)} + ${dx} + ${dy}`);
return dist;
});
log(distances.reduce((a, c) => a + c));
}
// part1(puzzle_input, 1);
part1(puzzle_input, 1000000 - 1);