-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday13.js
217 lines (185 loc) · 6.17 KB
/
day13.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
const fs = require('fs');
const main = async () => {
let input = fs.readFileSync('day13input.txt').toString().split(',');
input[0] = 2;
const output = getOutput(input);
let numblocks = 0;
// for (const tile of output) {
// if (tile.type === 2) {
// numblocks++;
// }
// }
//
// console.log(numblocks);
};
const getOutput = (program) => {
const matrix = [];
let tile = [];
let index = 0;
let running = true;
let relativeBase = 0;
while (running) {
let instruction = String(program[index]);
const firstNumIndex = Number(program.length > index+1 ? program[index+1] : null);
const secondNumIndex = Number(program.length > index+2 ? program[index+2] : null);
const answerPosition = Number(program.length > index+3 ? program[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(program, firstNumIndex, firstNumMode, relativeBase);
const secondNum = getValueWithMode(program, secondNumIndex, secondNumMode, relativeBase);
program[getAnswerPosition(answerPosition, answerPositionMode, relativeBase)] = firstNum + secondNum;
index = index + 4;
} else if (operation === '02') {
const firstNum = getValueWithMode(program, firstNumIndex, firstNumMode, relativeBase);
const secondNum = getValueWithMode(program, secondNumIndex, secondNumMode, relativeBase);
program[getAnswerPosition(answerPosition, answerPositionMode, relativeBase)] = firstNum * secondNum;
index = index + 4;
} else if (operation === '03') {
printMatrix(matrix);
const ballX = Number(getBallX(matrix));
const paddleX = Number(getPaddleX(matrix));
let direction;
if (ballX < paddleX) {
direction = -1;
} else if (ballX > paddleX) {
direction = 1;
} else {
direction = 0;
}
program[getAnswerPosition(firstNumIndex, firstNumMode, relativeBase)] = direction;
index = index + 2;
} else if (operation === '04') {
const firstNum = getValueWithMode(program, firstNumIndex, firstNumMode, relativeBase);
tile.push(firstNum);
if (tile.length === 3) {
if (Number(tile[0]) === -1 && Number(tile[1]) === 0) {
console.log('SCORE IS ', tile[2]);
} else {
addPointToMatrix(matrix, [tile[0], tile[1]], tile[2]);
}
tile = [];
}
index = index + 2;
} else if (operation === '99') {
running = false;
} else if (operation === '05') {
const firstNum = getValueWithMode(program, firstNumIndex, firstNumMode, relativeBase);
const secondNum = getValueWithMode(program, secondNumIndex, secondNumMode, relativeBase);
if (Number(firstNum) !== 0) {
index = secondNum;
} else {
index = index + 3;
}
} else if (operation === '06') {
const firstNum = getValueWithMode(program, firstNumIndex, firstNumMode, relativeBase);
const secondNum = getValueWithMode(program, secondNumIndex, secondNumMode, relativeBase);
if (Number(firstNum) === 0) {
index = secondNum;
} else {
index = index + 3;
}
} else if (operation === '07') {
const firstNum = getValueWithMode(program, firstNumIndex, firstNumMode, relativeBase);
const secondNum = getValueWithMode(program, secondNumIndex, secondNumMode, relativeBase);
program[getAnswerPosition(answerPosition, answerPositionMode, relativeBase)] = Number(firstNum) < Number(secondNum) ? 1 : 0;
index = index + 4;
} else if (operation === '08') {
const firstNum = getValueWithMode(program, firstNumIndex, firstNumMode, relativeBase);
const secondNum = getValueWithMode(program, secondNumIndex, secondNumMode, relativeBase);
program[getAnswerPosition(answerPosition, answerPositionMode, relativeBase)] = Number(firstNum) === Number(secondNum) ? 1 : 0;
index = index + 4;
} else if (operation === '09') {
const firstNum = getValueWithMode(program, firstNumIndex, firstNumMode, relativeBase);
relativeBase = relativeBase + firstNum;
index = index + 2;
} else {
throw new Error('bad instruction' + operation);
}
}
};
const addPointToMatrix = (matrix, point, item) => {
const x = Number(point[0]);
const y = Number(point[1]);
if (!matrix[y]) {
let xArray = [];
xArray[x] = item;
matrix[y] = xArray;
} else {
matrix[y][x] = item;
}
return matrix;
}
const getTileType = (tile) => {
if (tile === 0) {
return ' ';
} else if (tile === 1) {
return '|';
} else if (tile === 2) {
return '=';
} else if (tile === 3) {
return '_';
} else if (tile === 4) {
return 'O';
} else {
throw new Error('weird tile type ' + tile);
}
}
const getBallX = (matrix) => {
return getXOfType(matrix, 4);
}
const getPaddleX = (matrix) => {
return getXOfType(matrix, 3);
}
const getXOfType = (matrix, type) => {
for (const xArray in matrix) {
for (const item in matrix[xArray]) {
if (matrix[xArray][item] === type) {
return item;
}
}
}
throw Error('didnt find item of type ' + type);
}
const printMatrix = (matrix) => {
for (const xarray of matrix) {
let output = '';
for (const xItem of xarray) {
output = output + getTileType(xItem);
}
console.log(output);
}
}
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();