-
Notifications
You must be signed in to change notification settings - Fork 0
/
day3.ts
57 lines (45 loc) · 1.61 KB
/
day3.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
import * as fs from 'fs';
// read file
const readFile = (filePath: string) => {
return fs.readFileSync(filePath, 'utf8');
}
// regex for finding all mul(digit,digit) occurrences
const regex = /mul\((\d+),(\d+)\)/g;
const r_do = /do\(\)/g;
const r_dont = /don't\(\)/g;
type control = [boolean, number];
// get all mul operations with digits from string
const getMulOperations = (str: string) => {
const groups = str.matchAll(regex);
return groups;
}
// get all do positions
const doPositions = (str: string): control[] => {
const dos: control[] = [[true, -1], ...Array.from(str.matchAll(r_do)).map(m => [true, Number(m.index)] as control)];
console.log(dos);
const donts = Array.from(str.matchAll(r_dont)).map(m => [false, Number(m.index)] as control);
console.log(donts)
// merge dos and dont
const merged = dos.concat(donts).sort((a, b) => Number(a[1]) - Number(b[1]));
return merged;
}
// find maximum index before position
const maxIndex = (doPos: control[], pos: number): boolean => {
console.log('checking for pos:', pos);
for (let i = doPos.length - 1; i >= 0; i--) {
if (doPos[i][1] < pos) {
console.log(pos, doPos[i][0])
return doPos[i][0];
}
}
return false
}
// main
const file = readFile('resources/day3_input.txt');
const matches = Array.from(getMulOperations(file));
const p1 = matches.map(match => Number(match[1]) * Number(match[2])).reduce((s, v) => s + v, 0)
console.log('part 1:', p1);
const dos = doPositions(file);
console.log('dos:', dos);
const p2 = matches.filter(match => maxIndex(dos, match.index)).map(m => Number(m[1]) * Number(m[2])).reduce((s, v) => s + v, 0);
console.log('part 2:', p2);