-
Notifications
You must be signed in to change notification settings - Fork 17
/
part-one.js
34 lines (28 loc) · 905 Bytes
/
part-one.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
const { input } = require('./input');
const { InfiniteGrid } = require('./infinite-grid');
let grid = new InfiniteGrid({ defaultFactory: () => `.` });
grid.set(0, 0, 's');
let head = [0, 0];
let tail = [0, 0];
let tail_visted = new Set([InfiniteGrid.toId(...tail)]);
for (let { dir, steps } of input) {
for (let i = 0; i < steps; i++) {
let previousHead = head;
head = InfiniteGrid.moveInDirection(...head, dir);
grid.set(...head, 'H');
grid.set(...previousHead, '.');
grid.set(...tail, '.');
let neighbors = grid.neighbors(...head, true);
let neighbor_ids = new Set(
[...neighbors.values()].map((v) => v.id).concat(InfiniteGrid.toId(...head))
);
if (!neighbor_ids.has(InfiniteGrid.toId(...tail))) {
// Move tail
tail = previousHead;
tail_visted.add(InfiniteGrid.toId(...tail));
}
grid.set(...tail, 'T');
grid.set(0, 0, 's');
}
}
console.log(tail_visted.size);