-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday18.js
112 lines (100 loc) · 3.35 KB
/
day18.js
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
import { read, readEx, time, debug, assert_eq, createMatrix } from './util.ts';
import Vec from './util/Vec.ts';
import './util/prototype-shenanigans.js';
import './types.d.ts';
await readEx();
await read();
/** @type string **/
let data;
// 00:16:13
const partOne = () => {
let pos = data.lines().map(Vec.fromString);
console.log(pos, pos.length);
let width = debug ? 7 : 71;
let height = debug ? 7 : 71;
let N = debug ? 12 : 1024;
let grid = createMatrix(width, height, () => 0);
for (let i = 0; i < N; ++i) {
grid[pos[i].y][pos[i].x] = 1;
}
console.log(grid.map(l => l.map(b => b ? '#' : '.').join``).join`\n`);
let dist = createMatrix(width, height, () => Infinity);
let Q = [];
for (let y = 0; y < height; ++y) {
const row = grid[y];
for (let x = 0; x < width; ++x) {
if (row[x] === 1) continue;
dist[y][x] = Infinity;
Q.push(new Vec(x, y));
}
}
dist[0][0] = 0;
while (Q.length) {
const min = Q.map(u => dist[u.y][u.x]).min();
const ui = Q.findIndex(u => dist[u.y][u.x] === min);
const [u] = Q.splice(ui, 1);
const neighbours = [Vec.UP, Vec.DOWN, Vec.LEFT, Vec.RIGHT].map(v => v.add(u));
for (const v of neighbours) {
if (!Q.find(q => q.equals(v))) continue;
const alt = dist[u.y][u.x] + 1;
if (alt < dist[v.y][v.x]) {
dist[v.y][v.x] = alt;
}
}
}
console.log(dist[height - 1][width - 1]);
};
// 00:20:38
const partTwo = () => {
let pos = data.lines().map(Vec.fromString);
console.log(pos, pos.length);
let width = debug ? 7 : 71;
let height = debug ? 7 : 71;
for (let N = debug ? 12 : 1024; N < pos.length; ++N) {
let grid = createMatrix(width, height, () => 0);
for (let i = 0; i < N; ++i) {
grid[pos[i].y][pos[i].x] = 1;
}
// didn't intend it, but it looks really cool to see the points drawn in
console.log(grid.map(l => l.map(b => b ? '#' : '.').join``).join`\n`);
let dist = createMatrix(width, height, () => Infinity);
let Q = [];
for (let y = 0; y < height; ++y) {
const row = grid[y];
for (let x = 0; x < width; ++x) {
if (row[x] === 1) continue;
dist[y][x] = Infinity;
Q.push(new Vec(x, y));
}
}
dist[0][0] = 0;
while (Q.length) {
const min = Q.map(u => dist[u.y][u.x]).min();
const ui = Q.findIndex(u => dist[u.y][u.x] === min);
const [u] = Q.splice(ui, 1);
const neighbours = [Vec.UP, Vec.DOWN, Vec.LEFT, Vec.RIGHT].map(v => v.add(u));
for (const v of neighbours) {
if (!Q.find(q => q.equals(v))) continue;
const alt = dist[u.y][u.x] + 1;
if (alt < dist[v.y][v.x]) {
dist[v.y][v.x] = alt;
}
}
}
if (dist[height - 1][width - 1] === Infinity) {
console.log({ N }, pos[N - 1]);
break;
}
}
};
if (Deno.args[0]) {
console.log('Sample Data:');
data = await readEx();
console.log(data.lines().map(l => ' ' + l).join`\n`)
} else {
console.log('Real Data');
data = await read();
}
console.log('Output:');
time(partOne);
time(partTwo);