-
Notifications
You must be signed in to change notification settings - Fork 0
/
day15-part1.js
64 lines (54 loc) · 1.32 KB
/
day15-part1.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
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};
const inputFunction = () => {
if (steps.length === 0) {
const unexplored = area.search(droidPos.x, droidPos.y, undefined);
if (unexplored !== undefined) {
steps = unexplored.steps;
} else {
const path = area.search(0, 0, 2);
console.log(path.steps.length);
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,
};
switch (tile) {
case 0:
area.setTile(newDroidPos.x, newDroidPos.y, 0);
break;
case 1:
area.setTile(newDroidPos.x, newDroidPos.y, 1);
droidPos = newDroidPos;
break;
case 2:
area.setTile(newDroidPos.x, newDroidPos.y, 2);
droidPos = newDroidPos;
break;
}
}
});