-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.js
67 lines (54 loc) · 1.14 KB
/
day2.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
const fs = require('fs');
const input = fs.readFileSync('./input/day2.txt', 'utf8').trim().split(',');
const add = (a, b) => {
return a + b;
};
const multi = (a, b) => {
return a * b;
}
const run = (noun, verb) => {
const memory = [...input];
memory[1] = noun.toString();
memory[2] = verb.toString();
let running = true;
let index = 0;
while (running) {
const op = memory[index];
const a = parseInt(memory[memory[index + 1]]);
const b = parseInt(memory[memory[index + 2]]);
const loc = memory[index + 3];
switch (op) {
case '1':
memory[loc] = add(a, b);
break;
case '2':
memory[loc] = multi(a, b);
break;
case '99':
default:
running = false;
break;
}
index = index + 4;
}
return parseInt(memory[0]);
}
let found = false;
let noun = 12;
let verb;
const max = 99;
while (found == false) {
verb = 0;
for (let i = 0; i < 99; i++) {
verb = i;
if (run(noun, verb) === 19690720) {
found = true;
break;
}
}
if (!found) {
noun++;
}
}
const answer = (100 * noun) + verb;
console.info('Answer: ', answer);