-
Notifications
You must be signed in to change notification settings - Fork 0
/
day11-part2.js
93 lines (76 loc) · 1.69 KB
/
day11-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
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
const readline = require('readline');
const Computer = require('../common/intcode');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.once('line', line => {
const intCode = line.split(',').map(n => Number(n));
const computer = new Computer(intCode);
const iterator = computer.run();
const panels = new Map();
const getMapKey = pos => pos.x + ',' + pos.y;
let robotPos = {
x: 0,
y: 0,
};
let robotDir = {
x: 0,
y: -1,
};
panels.set(getMapKey(robotPos), 1);
const imageBounds = {
left: 0,
top: 0,
right: 0,
bottom: 0,
};
while (true) {
const mapKey = getMapKey(robotPos);
let input = panels.get(mapKey);
if (input === undefined) {
input = 0;
}
computer.enqueueInput(input);
let next = iterator.next();
if (next.done) {
break;
}
const color = next.value;
panels.set(mapKey, color);
next = iterator.next();
if (next.done) {
break;
}
const turn = next.value;
if (turn) {
robotDir = {
x: -robotDir.y,
y: robotDir.x,
};
} else {
robotDir = {
x: robotDir.y,
y: -robotDir.x,
};
}
robotPos.x += robotDir.x;
robotPos.y += robotDir.y;
imageBounds.left = Math.min(imageBounds.left, robotPos.x);
imageBounds.top = Math.min(imageBounds.top, robotPos.y);
imageBounds.right = Math.max(imageBounds.right, robotPos.x);
imageBounds.bottom = Math.max(imageBounds.bottom, robotPos.y);
}
let output = '';
for (let y = imageBounds.top; y <= imageBounds.bottom; ++y) {
for (let x = imageBounds.left; x <= imageBounds.right; ++x) {
if (panels.get(getMapKey({x, y}))) {
output += '#';
} else {
output += ' ';
}
}
output += '\n';
}
console.log(output);
});