-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path21.ts
165 lines (149 loc) · 5.2 KB
/
21.ts
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
import { getFileContent, getDataAsArray } from './utils.js';
interface Monkey {
name: string
number: number | null
job: string | null
}
const parseInput = (arr: string[], isPart1 = true) => {
const map = new Map<string, Monkey>();
arr.forEach(monkey => {
const [ name, jobOrNumber ] = monkey.split(': ');
let parsed = parseInt(jobOrNumber);
let number = Number.isNaN(parsed) ? null : parsed;
let job = Number.isNaN(parsed) ? ((!isPart1 && name === 'root') ? jobOrNumber.replace(/(\+|\-|\*|\/)/, '===') : jobOrNumber) : null;
let monkeyObj: Monkey = {
name,
number,
job
}
map.set(name, monkeyObj)
})
return map;
}
const buildReferenceMap = (map: Map<string, Monkey>) => {
const referenceMap = new Map<string, string[]>();
[...map.keys()].forEach(key => {
const tmp = map.get(key)!;
if(tmp.job !== null) {
const [left, _op, right] = tmp.job.split(' ');
if(!referenceMap.has(key)) referenceMap.set(key, []);
if(!referenceMap.has(left)) referenceMap.set(left, []);
if(!referenceMap.has(right)) referenceMap.set(right, []);
referenceMap.set(key, [...referenceMap.get(key)!, left, right])
referenceMap.set(left, [...referenceMap.get(left)!, key])
referenceMap.set(right, [...referenceMap.get(right)!, key])
}
})
return referenceMap;
}
const processMap = (map: Map<string, Monkey>) => {
const tmpMap = new Map(map);
let stack: Monkey[] = [tmpMap.get('root')!];
while(stack.length > 0) {
let currentMonkey = stack.pop()!;
if(currentMonkey.number !== null) {
continue;
}
const [ left, op, right ] = currentMonkey.job!.split(' ');
let leftMonkey = tmpMap.get(left)!
let rightMonkey = tmpMap.get(right)!
if(leftMonkey.number !== null && rightMonkey.number !== null) {
eval(`currentMonkey.number = leftMonkey.number ${op} rightMonkey.number`)
tmpMap.set(currentMonkey.name, currentMonkey);
continue;
}
stack.push(currentMonkey);
if(leftMonkey.number === null) {
stack.push(leftMonkey)
}
if(rightMonkey.number === null) {
stack.push(rightMonkey)
}
}
return tmpMap;
}
const jobBuilderByReference = (map: Map<string, Monkey>, referenceMap: Map<string, string[]>) => {
const tmpMap = new Map(map);
let stack: Monkey[] = [tmpMap.get('humn')!];
let alreadyVisited = new Set<string>();
while(stack.length > 0) {
let currentMonkey = stack.pop()!;
if(alreadyVisited.has(currentMonkey.name)) continue;
alreadyVisited.add(currentMonkey.name);
// console.log(currentMonkey);
let newReferences = referenceMap.get(currentMonkey.name)!;
let buildStr = currentMonkey.job === null ? '(humn)' : currentMonkey.job;
newReferences.forEach(ref => {
let changing = tmpMap.get(ref)!
if(changing.job === null) return;
changing.job = changing.job!.replace(ref, `(${buildStr})`);
tmpMap.set(ref, changing);
stack.push(tmpMap.get(ref)!);
});
}
return tmpMap;
}
const first = (arr: string[]) => {
const map = processMap(parseInput(arr));
const result = map.get('root')?.number;
console.log(result);
return result;
};
const second = (arr: string[]) => {
const map = parseInput(arr, false);
let count = 0;
let stack = ['root'];
let completed: string[] = [];
[...map.keys()].forEach(key => {
const tmp = map.get(key)!;
map.set(key, {...map.get(key)!, job: tmp.job ? tmp.job!.replace(/([a-z]+)/g, `($1)`) : null})
});
console.log(map);
while(stack.length > 0) {
let curName = stack.pop()!;
console.log("🚀 ~ file: 21.ts:116 ~ second ~ stack", stack)
if(completed.includes(curName)) continue;
let curObj = map.get(curName)!;
console.log("🚀 ~ file: 21.ts:118 ~ second ~ curObj", curObj)
if(curObj.job) {
let areNumbers = curObj.job.match(/\d+/g)
if(areNumbers) {
if(areNumbers.length === 1) {
completed.push(curName);
continue;
}
if(areNumbers.length === 2) {
let solution = 0;
eval(`solution = ${curObj.job}`);
map.set(curName, {...map.get(curName)!, job: `(${solution})`})
continue;
}
}
let matches: string[] | null = curObj.job.match(/[a-z]+/g)
if(!matches) continue;
matches = matches.filter(m => m !== 'humn')
let willGoToStack: string[] = [];
matches.forEach(match => {
const num = map.get(match)!.number
const job = map.get(match)!.job!
console.log("🚀 ~ file: 21.ts:140 ~ second ~ job", job)
if(num) {
map.set(curName, {...map.get(curName)!, job: map.get(curName)!.job!.replace(match, `${num}`)})
} else if(!Number.isNaN(parseInt(job))) {
map.set(curName, {...map.get(curName)!, job: map.get(curName)!.job!.replace(match, `${job}`)})
} else {
willGoToStack.unshift(match);
}
})
if(willGoToStack.length > 0) {
stack.push(curName);
willGoToStack.forEach(match => stack.push(match));
}
}
}
console.log(map);
return 0;
};
const data = getDataAsArray(getFileContent('example.txt'));
console.assert(first(data) === 0, 'Not matching first part');
console.assert(second(data) === 0, 'Not matching second part');