-
Notifications
You must be signed in to change notification settings - Fork 16
/
computer.js
54 lines (44 loc) · 918 Bytes
/
computer.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
const ADD = 1;
const MULTP = 2;
const STOP = 99;
class Computer {
constructor(input) {
this.input = input.slice(0);
}
run(noun, verb) {
let input = this.input.slice(0);
input[1] = noun;
input[2] = verb;
for (let i = 0; i < input.length; i += 4) {
let op = input[i];
let num1 = input[i + 1];
let num2 = input[i + 2];
let dest = input[i + 3];
if (op === ADD) {
input[dest] = input[num1] + input[num2];
} else if (op === MULTP) {
input[dest] = input[num1] * input[num2];
} else if (op === STOP) {
break;
}
}
return input[0];
}
partOne() {
return this.run(12, 2);
}
partTwo() {
let nums = Array(100)
.fill(0)
.map((c, i) => i);
for (let noun of nums) {
for (let verb of nums) {
let position_zero = this.run(noun, verb);
if (position_zero === 19690720) {
return 100 * noun + verb;
}
}
}
}
}
module.exports = Computer;