-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday7.js
153 lines (150 loc) · 4.15 KB
/
day7.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
149
150
151
152
// problem: http://adventofcode.com/day/7
// input: http://adventofcode.com/day/7/input
var input = document.body.innerText.split('\n');
var inputs = {}; // wire to (input) operator and operands or value
var outputs = {}; // wire to list of wires to notify when a value for this wire is available
function tryNumber(s) {
var n = Number.parseInt(s);
return isNaN(n) ? s : n
}
function parseInput(lhs) {
if (!lhs) {
return null;
}
var result = {};
var words = lhs.split(' ');
if (words[0] === 'NOT') {
result.operator = 'NOT';
result.operand = tryNumber(words[1]);
} else if (words[1] === 'LSHIFT' || words[1] === 'RSHIFT' || words[1] === 'AND' || words[1] === 'OR') {
result.operator = words[1];
result.operand1 = tryNumber(words[0]);
result.operand2 = tryNumber(words[2]);
} else {
result.operator = 'EQ';
result.operand = tryNumber(words[0]);
}
return result;
}
function tryLookup(maybeSymbol) {
if (typeof maybeSymbol === 'number') {
return maybeSymbol;
}
// see if this symbol resolves to a value
if (typeof inputs[maybeSymbol] === 'number') {
return inputs[maybeSymbol];
}
return maybeSymbol;
}
function appendOutput(rh, operand) {
if (outputs[operand] == null) {
outputs[operand] = [rh];
} else {
outputs[operand].push(rh);
}
}
function evalInput(lh, rh, append) {
var notify = false;
if (lh.operator === 'EQ') {
lh.operand = tryLookup(lh.operand);
if (typeof lh.operand === 'number') {
inputs[rh] = lh.operand;
notify = true;
} else if (append) {
appendOutput(rh, lh.operand);
}
} else if (lh.operator === 'NOT') {
lh.operand = tryLookup(lh.operand);
if (typeof lh.operand === 'number') {
inputs[rh] = 65535 - lh.operand;
notify = true;
} else if (append) {
appendOutput(rh, lh.operand);
}
} else if (lh.operator === 'LSHIFT') {
lh.operand1 = tryLookup(lh.operand1);
if (typeof lh.operand1 === 'number') {
inputs[rh] = lh.operand1 << lh.operand2;
notify = true;
} else if (append) {
appendOutput(rh, lh.operand1);
}
} else if (lh.operator === 'RSHIFT') {
lh.operand1 = tryLookup(lh.operand1);
if (typeof lh.operand1 === 'number') {
inputs[rh] = lh.operand1 >>> lh.operand2;
notify = true;
} else if (append) {
appendOutput(rh, lh.operand1);
}
} else if (lh.operator === 'AND') {
lh.operand1 = tryLookup(lh.operand1);
lh.operand2 = tryLookup(lh.operand2);
if (typeof lh.operand1 === 'number' && typeof lh.operand2 === 'number') {
inputs[rh] = lh.operand1 & lh.operand2;
notify = true;
} else {
if (append && typeof lh.operand1 !== 'number') {
appendOutput(rh, lh.operand1);
}
if (append && typeof lh.operand2 !== 'number') {
appendOutput(rh, lh.operand2);
}
}
} else if (lh.operator === 'OR') {
lh.operand1 = tryLookup(lh.operand1);
lh.operand2 = tryLookup(lh.operand2);
if (typeof lh.operand1 === 'number' && typeof lh.operand2 === 'number') {
inputs[rh] = lh.operand1 | lh.operand2;
notify = true;
} else {
if (append && typeof lh.operand1 !== 'number') {
appendOutput(rh, lh.operand1);
}
if (append && typeof lh.operand2 !== 'number') {
appendOutput(rh, lh.operand2);
}
}
}
if (notify && outputs[rh]) {
var k;
for (k = outputs[rh].length; k-->0; ) {
var listener = outputs[rh][k];
if (evalInput(inputs[listener], listener, false)) {
// listener EQ value and can stop listening
if (k > 0) {
outputs[rh].splice(k, 1);
} else {
delete outputs[rh];
}
}
}
}
return notify;
}
function parseLine(s) {
var h = s.split(' -> ');
var lh = parseInput(h[0]);
if (lh == null) {
return;
}
var rh = h[1];
inputs[rh] = lh;
evalInput(lh, rh, true);
}
var i;
for (i = 0; i < input.length; i++) {
parseLine(input[i]);
}
console.log(inputs.a);
// part b: substitute 956 -> b
inputs = {};
outputs = {};
for (i = 0; i < input.length; i++) {
if (input[i] === '14146 -> b') {
parseLine('956 -> b');
} else {
parseLine(input[i]);
}
}
console.log(inputs.a);