-
Notifications
You must be signed in to change notification settings - Fork 15
/
parallel.js
148 lines (121 loc) · 3.79 KB
/
parallel.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Helper, I use this a bit
const isStr = val => typeof val === 'string';
class Program {
constructor(id, program) {
this.id = id;
this.queue = [];
this.sibling;
this.program = JSON.parse(JSON.stringify(program));
this.registers = Program.getUniqueRegisters(this.program);
this.registers.p = this.id;
this.index = 0;
this.finished = false;
this.sent_count = 0;
}
setSibling(sibling) {
this.sibling = sibling;
}
addToQueue(value) {
this.queue.push(value);
}
snd(value) {
this.sent_count++;
this.sibling.addToQueue(this.getValueAtRegister(value));
}
set(register, value) {
this.registers[register] = this.getValueAtRegister(value);
}
add(register, value) {
this.registers[register] += this.getValueAtRegister(value);
}
mul(register, value) {
this.registers[register] *= this.getValueAtRegister(value);
}
mod(register, value) {
this.registers[register] %= this.getValueAtRegister(value);
}
canReceive() {
return Boolean(this.queue.length);
}
rcv(register) {
if (this.queue.length > 0) {
// Set register to the value at top of the queue
this.registers[register] = this.queue.shift();
return false;
} else {
// Halt the program, nothing to receive
this.finished = true;
return true;
}
}
jgz(value, offset) {
if (this.getValueAtRegister(value) > 0) {
this.index += this.getValueAtRegister(offset);
return true;
} else {
return false;
}
}
static getUniqueRegisters(program, return_object = true) {
let registers = {};
for (let instruction of program) {
let { x, y } = instruction;
if (isStr(x)) {
registers[x] = 0;
}
if (isStr(y)) {
registers[y] = 0;
}
}
return return_object ? registers : Object.keys(registers);
}
getValueAtRegister(value) {
return isStr(value) ? this.registers[value] : value;
}
tick() {
let { type, x, y } = this.program[this.index];
let value = this[type](x, y);
if (type === 'jgz' && value === true) {
// Do nothing, we jumped. Otherwise, skip the `jgz` instruction
} else {
this.index++;
}
return this.last_sound_played;
}
peekCurrentInstructionType() {
return this.program[this.index].type;
}
}
class ParallelPrograms {
constructor(program) {
this.programs = [
new Program(0, program),
new Program(1, program),
];
this.programs[0].setSibling(this.programs[1]);
this.programs[1].setSibling(this.programs[0]);
}
orchestrateRun() {
let current_program = 0;
let flip_count = 0;
let p = this.programs[current_program];
do {
if (p.peekCurrentInstructionType() === 'rcv' && !p.canReceive()) {
flip_count++;
if (flip_count >= 2) {
// Flipped twice, neither program can receive, so we should be deadlocked
return this.programs[1].sent_count;
} else {
// Hacky way to toggle between 0 and 1
current_program = +!current_program;
p = this.programs[current_program];
}
} else {
flip_count = 0;
p.tick();
}
} while (!this.programs[0].finished || !this.programs[1].finished);
return this.programs[1].sent_count;
}
}
module.exports = ParallelPrograms;