-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday52.js
More file actions
77 lines (70 loc) · 1.5 KB
/
Copy pathday52.js
File metadata and controls
77 lines (70 loc) · 1.5 KB
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
const fs = require("fs");
let input = fs.readFileSync("./day5.txt").toString().split(',');
const c = input => {
let i = 0;
while (i < input.length) {
let jump = 1;
let e = input[i].toString();
let a = ~~input[i+1];
let b = ~~input[i+2];
let c = ~~input[i+3];
let modes = [];
if (e.length > 1) {
opcode = e.substr(-2);
modes = e.substr(0, e.length-2).split('').map(e => ~~e);
} else opcode = e;
opcode = ~~opcode
while(modes.length < 3) modes.unshift(0);
if (opcode != 3 && opcode != 4) {
if (!modes[1]) b = ~~input[b];
if (!modes[2]) a = ~~input[a];
}
switch(opcode) {
case 1:
jump+=3;
input[c]=a+b;
break;
case 2:
jump+=3;
input[c]=a*b;
break;
case 3:
jump++;
input[a]=5
break;
case 4:
jump++;
console.log(input[a]);
return;
break;
case 5:
console.log({ a })
if (a) {
i=b-jump;
} else jump+=2;
break;
case 6:
if (!a) {
i=b-jump;
} else jump+=2;
break;
case 7:
jump += 3;
input[c] = (a<b) ? 1 : 0;
break;
case 8:
jump += 3;
input[c] = (a==b) ? 1 : 0;
break;
default:
console.log({ opcode });
console.log("Invalid opcode!");
case 99:
i=input.length+1;
}
i+=jump;
console.log({ input })
}
return ~~input[0];
}
c(input);