-
Notifications
You must be signed in to change notification settings - Fork 0
/
day15-part2.js
59 lines (49 loc) · 1.2 KB
/
day15-part2.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
const readline = require('readline');
const Computer = require('../common/intcode');
const {Area, directions} = require('./Area');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.once('line', line => {
const intCode = line.split(',').map(n => Number(n));
let dir;
let steps = [];
const area = new Area();
let droidPos = {x: 0, y: 0};
let systemPos;
const inputFunction = () => {
if (steps.length === 0) {
const unexplored = area.search(droidPos.x, droidPos.y, undefined);
if (unexplored !== undefined) {
steps = unexplored.steps;
} else {
const dist = area.maxDist(systemPos.x, systemPos.y);
console.log(dist);
process.exit(0);
}
}
dir = steps.shift();
return dir + 1;
};
const computer = new Computer(intCode, inputFunction);
const iterator = computer.run();
while (true) {
let next = iterator.next();
if (next.done) {
break;
}
const tile = next.value;
const newDroidPos = {
x: droidPos.x + directions[dir].x,
y: droidPos.y + directions[dir].y,
};
area.setTile(newDroidPos.x, newDroidPos.y, tile);
if (tile !== 0) {
droidPos = newDroidPos;
}
if (tile === 2) {
systemPos = droidPos;
}
}
});