-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day18.js
145 lines (140 loc) · 2.96 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
133
134
135
136
137
138
139
140
141
142
143
144
145
fs = require('fs');
fs.readFile('Day18.txt', 'utf8', function(err, data) {
if(err) {
return console.log(err);
}
data = data.trim();
let directions = data.split('\n');
let length = directions.length;
let index1 = 0;
let index2 = 0;
let deadlock = false;
let toggle = true;
let register1 = new Set();
register1['p'] = 0;
let register2 = new Set();
register2['p'] = 1;
let queue1 = [];
let queue2 = [];
let sound = null;
let sent = 0;
let count = 0;
function getVal(match) {
let matchInt = parseInt(match);
if(isNaN(matchInt)) {
if(toggle) {
if (register1[`${match}`])
return register1[`${match}`];
else
return 0;
}
else {
if (register2[`${match}`])
return register2[`${match}`];
else
return 0;
}
}
else return matchInt;
}
while(!deadlock) {
let pattern = /^(\w+)\s(\w)(\s(.+))?$/;
let match = toggle ? directions[index1].match(pattern) : directions[index2].match(pattern);
switch(match[1]) {
case 'set':
if(toggle) {
register1[`${match[2]}`] = getVal(match[4]);
index1++;
}
else {
register2[`${match[2]}`] = getVal(match[4]);
index2++;
}
break;
case 'add':
if(toggle) {
register1[`${match[2]}`] = getVal(match[2]) + getVal(match[4]);
index1++;
}
else {
register2[`${match[2]}`] = getVal(match[2]) + getVal(match[4]);
index2++;
}
break;
case 'mul':
if(toggle) {
register1[`${match[2]}`] = getVal(match[2]) * getVal(match[4]);
index1++;
}
else {
register2[`${match[2]}`] = getVal(match[2]) * getVal(match[4]);
index2++;
}
break;
case 'mod':
if(toggle) {
register1[`${match[2]}`] = getVal(match[2]) % getVal(match[4]);
index1++;
}
else {
register2[`${match[2]}`] = getVal(match[2]) % getVal(match[4]);
index2++;
}
break;
case 'snd':
if(toggle) {
queue2.push(getVal(match[2]));
index1++;
}
else {
queue1.push(getVal(match[2]));
sent++;
index2++;
}
break;
case 'rcv':
if(toggle) {
if(queue1[0] !== undefined ) {
register1[`${match[2]}`] = queue1[0];
queue1.length > 1 ? queue1.shift() : queue1 = [];
index1++;
}
else {
if(queue1[0] === undefined && queue2[0] === undefined) deadlock = true;
toggle = false;
}
}
else {
if(queue2[0] !== undefined) {
register2[`${match[2]}`] = queue2[0];
queue2.length > 1 ? queue2.shift() : queue2 = [];
index2++;
}
else {
if(queue1[0] === undefined && queue2[0] === undefined) deadlock = true;
toggle = true;
}
}
break;
case 'jgz':
if(toggle) {
if(getVal(match[2]) > 0) {
index1 += getVal(match[4]);
}
else index1++;
}
else {
if(getVal(match[2]) > 0) {
index2 += getVal(match[4]);
}
else index2++;
}
break;
}
index1 %= length;
index2 %= length;
count++;
}
//console.log(sound);
console.log(sent);
});