-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day18.js
132 lines (118 loc) · 3.1 KB
/
Day18.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const part1 = (input) => {
let registers = {},
lastSound = 0,
ptr = 0,
i = 0;
const code = input.split(/\n+/).map(l => l.split(/\s+/));
const regorn = (x) => {
return x.match && x.match(/^[a-z]$/) ? getreg(x) : +x;
};
const getreg = (r) => {
if (typeof registers[r] === 'undefined') {
registers[r] = 0;
}
return registers[r];
};
const setreg = (r, v) => registers[r] = regorn(v);
const ops = {
'snd': (x) => lastSound = regorn(x),
'set': (r, x) => setreg(r, x),
'add': (r, x) => setreg(r, getreg(r) + regorn(x)),
'mul': (r, x) => setreg(r, getreg(r) * regorn(x)),
'mod': (r, x) => setreg(r, getreg(r) % regorn(x)),
'rcv': (r) => getreg(r),
'jgz': (r, y) => {
if (getreg(r) > 0) {
ptr += regorn(y);
} else {
ptr++;
}
}
};
for (ptr = 0; ptr < code.length && i < 1e5; i++) {
let op = code[ptr],
res = ops[op[0]](op[1], op[2]);
if (op[0] === 'rcv' && res !== 0) {
console.log('A:', lastSound);
return;
}
if (op[0] !== 'jgz') {
ptr++;
}
}
};
class Prg {
registers = {}
ptr = 0
queue = []
code = []
id = 0
sent = 0
sibling = 0
constructor(id, input) {
this.id = id;
this.code = input.split(/\n+/).map(l => l.trim().split(/\s+/));
}
setSibling(sibling) {
this.sibling = sibling;
}
isStalled() {
return this.code[this.ptr][0] === 'rcv' && this.queue.length <= 0;
}
isDone() {
return this.ptr >= this.code.length - 1;
}
isStalledOrDone() {
return this.isDone() || this.isStalled();
}
step() {
const regorn = (x) => {
return x.match && x.match(/^[a-z]$/) ? getreg(x) : +x;
};
const getreg = (r) => {
if (typeof this.registers[r] === 'undefined') {
this.registers[r] = r === 'p' ? this.id : 0;
}
return this.registers[r];
};
const setreg = (r, v) => this.registers[r] = regorn(v);
const ops = {
'snd': (x) => {
this.sibling.queue.push(regorn(x));
this.ptr++
this.sent++;
},
'set': (r, x) => { setreg(r, x); this.ptr++ },
'add': (r, x) => { setreg(r, getreg(r) + regorn(x)); this.ptr++ },
'mul': (r, x) => { setreg(r, getreg(r) * regorn(x)); this.ptr++ },
'mod': (r, x) => { setreg(r, getreg(r) % regorn(x)); this.ptr++ },
'rcv': (r) => {
if (this.queue.length > 0) {
this.registers[r] = this.queue.shift();
this.ptr++
}
},
'jgz': (r, y) => {
if (regorn(r) > 0) {
this.ptr += regorn(y);
} else {
this.ptr++;
}
}
}
ops[this.code[this.ptr][0]](this.code[this.ptr][1], this.code[this.ptr][2]);
}
}
const part2 = (input) => {
const programs = [new Prg(0, input), new Prg(1, input)];
programs[0].setSibling(programs[1]);
programs[1].setSibling(programs[0]);
do {
programs.forEach(p => {
if (!p.isDone()) {
p.step();
}
})
} while (!programs.reduce((a, b) => a && b.isStalledOrDone(), true));
console.log('B:', programs[1].sent);
};