-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_2.js
68 lines (57 loc) · 1.34 KB
/
day_2.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
var fs = require('fs');
var timeStarted = Date.now();
var input = fs.readFileSync('input_2.txt', 'utf-8');
var inputIntCode = input.split(',').map(function(val) {return parseInt(val)});
//First steps
// inputIntCode[1] = 12;
// inputIntCode[2] = 2;
let x = 0, y = 0;
//Brute force find the inputs
while (x < 100) {
while (y < 100) {
let newIntcode = inputIntCode.slice();
newIntcode[1] = x;
newIntcode[2] = y;
let output = processIntcode(newIntcode)[0];
if (output === 19690720) {
console.log('x', x, 'y', y);
}
y++;
}
y = 0;
x++;
}
process.exit(0);
function processIntcode(intcode) {
let index = 0;
let isHalted = false;
while (index < intcode.length && !isHalted) {
_processChunk(...getChunk(index));
index += 4;
}
return intcode;
function _processChunk(a, b, c, d) {
switch(a) {
case 1:
intcode[d] = intcode[b] + intcode[c];
break;
case 2:
intcode[d] = intcode[b] * intcode[c];
break;
case 99:
isHalted = true;
break;
default:
console.log('Something went wrong');
}
}
function getChunk(startingIndex) {
let returnValues = [];
var x = 0;
while (startingIndex + x < intcode.length && x < 4) {
returnValues.push(intcode[startingIndex + x]);
x++;
}
return returnValues;
}
}