-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day23.js
76 lines (65 loc) · 1.43 KB
/
Day23.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
69
70
71
72
73
74
75
76
const part1 = (code) => {
const instructions = code.split(/\n+/).map(l => l.split(/\s+/));
let registers = {},
ptr = 0,
numInst = {};
const getOp = (op) => {
if (op.match(/[a-h]/)) {
if (typeof registers[op] === 'undefined') {
registers[op] = 0;
}
return registers[op];
} else {
return +op;
}
};
const instInvoked = (inst) => {
if (typeof numInst[inst] === 'undefined') {
numInst[inst] = 0;
}
numInst[inst]++;
}
const handleInst = ([inst, opA, opB]) => {
const a = getOp(opA),
b = getOp(opB);
instInvoked(inst);
console.log(`${inst} ${opA} (${a}) ${opB} (${b})`);
switch (inst) {
case 'set': registers[opA] = b; break;
case 'sub': registers[opA] -= b; break;
case 'mul': registers[opA] *= b; break;
case 'jnz':
if (a !== 0) {
ptr--;
ptr += b;
}
break;
}
};
for(let i = 0; ptr < instructions.length; i++) {
handleInst(instructions[ptr]);
ptr++;
}
console.log('A:', numInst.mul);
};
const part2 = () => {
let a=1,d=0,e=0,f=0,g=0,h=0,
b = (79 * 100) + 100000,
c = b + 17000;
do {
f = 1;
e = 2;
for (d = 2; d * d <= b; d++) {
if (b % d === 0) {
f = 0;
break;
}
}
if (f == 0) {
h++;
}
g = b - c;
b += 17;
} while (g != 0);
console.log('B:', h);
};