-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday11.js
200 lines (168 loc) · 6.22 KB
/
day11.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
const fs = require('fs');
const main = async () => {
let input = fs.readFileSync('day11input.txt').toString().split(',');
getOutput(input);
};
const getOutput = (input) => {
const colorMap = {};
let colorPoint = [0, 0];
let settingColor = true;
let direction = 90;
let index = 0;
let running = true;
let relativeBase = 0;
while (running) {
let instruction = String(input[index]);
const firstNumIndex = Number(input.length > index+1 ? input[index+1] : null);
const secondNumIndex = Number(input.length > index+2 ? input[index+2] : null);
const answerPosition = Number(input.length > index+3 ? input[index+3] : null);
while (instruction.length < 5) {
instruction = '0' + String(instruction);
}
const firstNumMode = instruction[2];
const secondNumMode = instruction[1];
const answerPositionMode = instruction[0];
const operation = String(instruction).substr(3, 5);
if (operation === '01') {
const firstNum = getValueWithMode(input, firstNumIndex, firstNumMode, relativeBase);
const secondNum = getValueWithMode(input, secondNumIndex, secondNumMode, relativeBase);
input[getAnswerPosition(answerPosition, answerPositionMode, relativeBase)] = firstNum + secondNum;
index = index + 4;
} else if (operation === '02') {
const firstNum = getValueWithMode(input, firstNumIndex, firstNumMode, relativeBase);
const secondNum = getValueWithMode(input, secondNumIndex, secondNumMode, relativeBase);
input[getAnswerPosition(answerPosition, answerPositionMode, relativeBase)] = firstNum * secondNum;
index = index + 4;
} else if (operation === '03') {
// different input
let value = colorMap[colorPoint[0] + ':' + colorPoint[1]];
if (!value) value = 1;
input[getAnswerPosition(firstNumIndex, firstNumMode, relativeBase)] = value;
index = index + 2;
} else if (operation === '04') {
const firstNum = getValueWithMode(input, firstNumIndex, firstNumMode, relativeBase);
if (settingColor) {
colorMap[colorPoint[0] + ':' + colorPoint[1]] = Number(firstNum);
} else {
direction = updateDirection(firstNum, direction);
colorPoint = moveOne(direction, colorPoint);
}
settingColor = !settingColor;
index = index + 2;
} else if (operation === '99') {
running = false;
} else if (operation === '05') {
const firstNum = getValueWithMode(input, firstNumIndex, firstNumMode, relativeBase);
const secondNum = getValueWithMode(input, secondNumIndex, secondNumMode, relativeBase);
if (Number(firstNum) !== 0) {
index = secondNum;
} else {
index = index + 3;
}
} else if (operation === '06') {
const firstNum = getValueWithMode(input, firstNumIndex, firstNumMode, relativeBase);
const secondNum = getValueWithMode(input, secondNumIndex, secondNumMode, relativeBase);
if (Number(firstNum) === 0) {
index = secondNum;
} else {
index = index + 3;
}
} else if (operation === '07') {
const firstNum = getValueWithMode(input, firstNumIndex, firstNumMode, relativeBase);
const secondNum = getValueWithMode(input, secondNumIndex, secondNumMode, relativeBase);
input[getAnswerPosition(answerPosition, answerPositionMode, relativeBase)] = Number(firstNum) < Number(secondNum) ? 1 : 0;
index = index + 4;
} else if (operation === '08') {
const firstNum = getValueWithMode(input, firstNumIndex, firstNumMode, relativeBase);
const secondNum = getValueWithMode(input, secondNumIndex, secondNumMode, relativeBase);
input[getAnswerPosition(answerPosition, answerPositionMode, relativeBase)] = Number(firstNum) === Number(secondNum) ? 1 : 0;
index = index + 4;
} else if (operation === '09') {
const firstNum = getValueWithMode(input, firstNumIndex, firstNumMode, relativeBase);
relativeBase = relativeBase + firstNum;
index = index + 2;
} else {
throw new Error('bad instruction' + operation);
}
}
console.log('num painted ', Object.keys(colorMap).length);
printGrid(colorMap);
return input[0];
};
const printGrid = (colorMap) => {
const keys = Object.keys(colorMap);
const xValues = Array.from(new Set(keys.map(key => {
return Number(key.split(':')[0]);
})));
const yValues = Array.from(new Set(keys.map(key => {
return Number(key.split(':')[1]);
})));
for (let y = Math.min(...yValues); y <= Math.max(...yValues); y++) {
let oneLine = '';
for (let x = Math.min(...xValues); x <= Math.max(...xValues); x++) {
const key = x + ':' + y;
if (colorMap[key] === 'undefined') {
oneLine = oneLine + ' ' + '|';
} else {
oneLine = oneLine + ' ' + (colorMap[key] === 1 ? '|' : 'o');
}
}
console.log(oneLine);
}
}
const moveOne = (direction, colorPoint) => {
if (direction === 0) {
return [colorPoint[0] - 1, colorPoint[1]];
} else if (direction === 90) {
return [colorPoint[0], colorPoint[1] + 1]
} else if (direction === 180) {
return [colorPoint[0] + 1, colorPoint[1]];
} else if (direction === 270) {
return [colorPoint[0], colorPoint[1] - 1];
}
};
const updateDirection = (num, direction) => {
let change;
if (num === 0) {
change = -90;
} else if (num === 1) {
change = 90;
} else {
throw new Error('invalid direction change ' + num);
}
let newDirection = direction + change;
if (newDirection < 0) {
newDirection = newDirection + 360;
} else if (newDirection >= 360) {
newDirection = newDirection - 360;
}
return newDirection;
};
const getAnswerPosition = (index, mode, relativeBase) => {
if (mode === '0') {
return index;
} else if (mode === '2') {
return relativeBase + index;
} else {
throw new Error('the answer mode is weird ' + mode);
}
}
const getValueWithMode = (program, index, mode, relativeBase) => {
if (mode === '0') {
if (index >= program.length) {
return 0;
} else {
return Number(program[index]);
}
} else if (mode === '1') {
return Number(index);
} else if (mode === '2') {
let nextIndex = index + relativeBase;
if (nextIndex >= program.length) {
return 0;
} else {
return Number(program[nextIndex]);
}
}
}
main();