-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
66 lines (55 loc) · 1.66 KB
/
index.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
import run from "aocrunner";
const parseInput = (rawInput: string) => rawInput;
const calculate = (input: string) => {
// get all numbers from string
const numbers = input.match(/\d{1,3}/g);
return numbers?.reduce((acc, num) => acc * parseInt(num), 1) ?? 0;
};
const part1 = (rawInput: string) => {
const input = parseInput(rawInput);
// regex pattern to match mul and two numbers with one to three digits
const mulPattern = /mul\((\d{1,3}),(\d{1,3})\)/g;
const matches = input.match(mulPattern);
// sum up all the results of the calculations
return matches?.reduce((acc, match) => acc + calculate(match), 0);
};
const part2 = (rawInput: string) => {
const input = parseInput(rawInput);
// regex pattern to match mul and two numbers with one to three digits or do() or don't()
const mulPattern = /mul\((\d{1,3}),(\d{1,3})\)|do\(\)|don't\(\)/g;
const matches = input.match(mulPattern);
let result = 0;
let shouldCalculate = true;
matches?.forEach((match) => {
if (shouldCalculate && match === "don't()") {
shouldCalculate = false;
} else if (!shouldCalculate && match === "do()") {
shouldCalculate = true;
} else if (shouldCalculate) {
result += calculate(match) ?? 0;
}
});
return result;
};
run({
part1: {
tests: [
{
input: `xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))`,
expected: 161,
},
],
solution: part1,
},
part2: {
tests: [
{
input: `xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))`,
expected: 48,
},
],
solution: part2,
},
trimTestInputs: true,
onlyTests: false,
});