-
Notifications
You must be signed in to change notification settings - Fork 0
/
p2.js
53 lines (43 loc) · 926 Bytes
/
p2.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
const fs = require('fs')
//'./p2_data.txt'
fs.readFile('./p2_data.txt', 'utf8', function(err, data) {
let input = data.split(',').map(Number)
for(let i = 0; i <= 99; i++) {
for(let j = 0; j <= 99; j++) {
let result = run_program([... input], i, j)
if(result == 19690720) {
console.log(100*i+j)
}
}
}
})
function run_program(input, noun, verb) {
let opCodes = {
1: add,
2: multiply,
}
input[1] = noun
input[2] = verb
let head = 0
while(head < input.length) {
if(input[head] == 99) break
let op = opCodes[input[head]]
if(op) {
op(input[head+1], input[head+2], input[head+3], input)
head+=4
}
else {
head++
}
}
return input[0]
}
function add(a, b, p, arr) {
arr[p] = Number(arr[a])+Number(arr[b])
}
function multiply(a, b, p, arr) {
arr[p] = Number(arr[a])*Number(arr[b])
}
function halt(arr) {
return arr
}