-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.ts
113 lines (102 loc) · 2.8 KB
/
11.ts
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
113
import {IntcodeComputer} from './intcode';
import {parseIntcode} from './utils';
/*
https://adventofcode.com/2019/day/2
*/
/**
* Run an Intcode computer that paints white and black squares.
* Returns a map of the squares that have been painted.
*/
const PaintingRobot = (
initialConditions: {[coordinate: string]: number},
program: Array<number>
): {[coordinate: string]: number} => {
let pos = [0, 0];
let direction = 'N';
const paintedPanels: {[coordinate: string]: number} = initialConditions;
const currentPanelColor = () => {
return paintedPanels[pos.join('|')] || 0;
};
const turn = (turnRight: number) => {
switch (direction) {
case 'N':
direction = turnRight ? 'E' : 'W';
break;
case 'E':
direction = turnRight ? 'S' : 'N';
break;
case 'S':
direction = turnRight ? 'W' : 'E';
break;
case 'W':
direction = turnRight ? 'N' : 'S';
break;
}
};
const move = () => {
switch (direction) {
case 'N':
pos = [pos[0]!, pos[1]! - 1];
break;
case 'E':
pos = [pos[0]! + 1, pos[1]!];
break;
case 'S':
pos = [pos[0]!, pos[1]! + 1];
break;
case 'W':
pos = [pos[0]! - 1, pos[1]!];
break;
}
};
const computer = new IntcodeComputer(program);
computer.addInput(currentPanelColor());
while (!computer.isHalted()) {
paintedPanels[pos.join('|')] = computer.run();
turn(computer.run());
move();
computer.addInput(currentPanelColor());
}
return paintedPanels;
};
/**
* Run a painting robot program and count the number of painted squares
*/
export const part1 = (program: Array<number>): number => {
return Object.keys(PaintingRobot({}, program)).length;
};
/**
* Run a painting robot program and return a printable output.
*/
export const part2 = (program: Array<number>): string => {
const result = PaintingRobot({'0|0': 1}, program);
const minX = Object.keys(result).reduce(
(min, coord) => Math.min(min, parseInt(coord.split('|')[0]!)),
Infinity
);
const minY = Object.keys(result).reduce(
(min, coord) => Math.min(min, parseInt(coord.split('|')[1]!)),
Infinity
);
const maxX = Object.keys(result).reduce(
(max, coord) => Math.max(max, parseInt(coord.split('|')[0]!)),
-Infinity
);
const maxY = Object.keys(result).reduce(
(max, coord) => Math.max(max, parseInt(coord.split('|')[1]!)),
-Infinity
);
let painting = '';
for (let y = minY; y <= maxY; y += 1) {
for (let x = minX; x <= maxX; x += 1) {
painting += result[`${x}|${y}`] ? '■' : ' ';
}
painting += '\n';
}
return painting;
};
/**
* Parse the puzzle input file ready for processing
*/
/* istanbul ignore next */
export const parse = (): Promise<Array<number>> => parseIntcode(11);