-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday21.js
178 lines (148 loc) · 4.37 KB
/
day21.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
const fs = require('fs');
class Monkey {
constructor(id, neighbors, operation) {
this.id = id;
this.neighbors = [...neighbors];
this.operation = operation;
}
get isInteger() {
return !this.operation;
}
getValue(dict) {
if (!this.operation) return this.neighbors[0];
let key1 = this.neighbors[0];
let key2 = this.neighbors[1];
let value1 = dict[key1].getValue(dict);
let value2 = dict[key2].getValue(dict);
switch (this.operation) {
case '+': return value1 + value2;
case '-': return value1 - value2;
case '*': return value1 * value2;
case '/': return value1 / value2;
}
}
toString() {
if (!this.operation) return this.neighbors[0];
let key1 = this.neighbors[0];
let key2 = this.neighbors[1];
return `${key1} ${this.operation} ${key2}`;
}
}
const day21a = () => {
// const filename = './data/day21test.txt'; // 152
const filename = './data/day21.txt'; // 84244467642604
const dict = buildGraph(filename);
console.log('day21a = ', dict['root'].getValue(dict));
}
const day21b = () => {
// const filename = './data/day21test.txt'; // 152
const filename = './data/day21.txt'; // 84244467642604
const dict = buildGraph(filename);
// generate the path to humn
let root = 'root';
const paths = dfs(root, 'humn', dict);
let path = paths[0];
// console.log(path);
let rootNode, targetKey, targetValue;
for (const neighbor of dict['root'].neighbors) {
if (path.includes(neighbor)) {
rootNode = neighbor;
} else {
targetValue = dict[neighbor].getValue(dict);
}
}
// console.log(`set ${rootNode} to ${targetValue}`);
dict['humn'].neighbors = ['???'];
dict['humn'].operation = '???';
const queue = [[dict[rootNode], targetValue]];
while (queue.length > 0) {
[rootNode, targetValue] = queue.pop();
targetKey = rootNode.neighbors.find(n => path.includes(n));
[rootNode, targetValue] = solve(rootNode, targetKey, targetValue, dict);
if (rootNode.id === 'humn') {
console.log('day21b = ', targetValue);
return;
}
queue.push([rootNode, targetValue]);
}
}
const solve = (rootNode, targetKey, targetValue, dict) => {
const operation = rootNode.operation;
const nodeA = dict[rootNode.neighbors[0]];
const nodeB = dict[rootNode.neighbors[1]];
let newValue = 0;
let nodePtr;
switch (operation) {
case '+':
if (nodeA.id === targetKey) {
newValue = targetValue - nodeB.getValue(dict);
nodePtr = nodeA;
} else {
newValue = targetValue - nodeA.getValue(dict);
nodePtr = nodeB;
}
break;
case '-':
if (nodeA.id === targetKey) {
newValue = targetValue + nodeB.getValue(dict);
nodePtr = nodeA;
} else {
newValue = nodeA.getValue(dict) - targetValue;
nodePtr = nodeB;
}
break;
case "*":
if (nodeA.id === targetKey) {
newValue = targetValue / nodeB.getValue(dict);
nodePtr = nodeA;
} else {
newValue = targetValue / nodeA.getValue(dict);
nodePtr = nodeB;
}
break;
case "/":
if (nodeA.id === targetKey) {
newValue = targetValue * nodeB.getValue(dict);
nodePtr = nodeA;
} else {
newValue = targetValue / nodeA.getValue(dict);
nodePtr = nodeB;
}
break;
}
return [nodePtr, newValue];
}
const dfs = (start, end, dict) => {
if (start === end) return [[]];
if (dict[start].isInteger) return null;
const paths = [];
for (let neighbor of dict[start].neighbors) {
const localPaths = dfs(neighbor, end, dict);
if (localPaths !== null) {
for (const path of localPaths) {
paths.push([...path, neighbor]);
}
}
}
return paths;
}
const buildGraph = (filename) => {
const dict = {};
let arr = fs.readFileSync(filename,
{ encoding: 'utf8', flag: 'r' }).split('\n');
for (let line of arr) {
const key = line.split(':')[0];
const value = line.split(':')[1];
if (Number.isInteger(+value)) {
dict[key] = new Monkey(key, [+value], null);
} else {
const components = value.trimStart().split(' ');
const comp1Key = components[0];
const operation = components[1];
const comp2Key = components[2];
dict[key] = new Monkey(key, [comp1Key, comp2Key], operation);
}
}
return dict;
}
module.exports = { day21a, day21b };