-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.ts
54 lines (46 loc) · 1.41 KB
/
3.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
import * as fs from "fs";
// const input = "xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))";
// const input = "xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))";
const input = fs.readFileSync("3-input.txt", 'utf8');
const part1 = () => {
const regexStr = "mul\\([0-9]+,[0-9]+\\)";
const regex = new RegExp(regexStr, "g");
const groups = input.match(regex);
return groups?.reduce((acc, s) => {
const nums = s
.split("(")[1]
.split(")")[0]
.split(",")
.map((x) => Number(x));
const res = nums[0] * nums[1];
return acc + res;
}, 0);
};
const part2 = () => {
const regexStr = "mul\\([0-9]+,[0-9]+\\)|don\\'t\\(\\)|do\\(\\)";
const regex = new RegExp(regexStr, "g");
const groups = input.match(regex);
let enabled = true;
return groups?.reduce((acc, s) => {
if (s === "do()") {
enabled = true;
return acc;
}
if (s === "don't()") {
enabled = false;
return acc;
}
if (enabled) {
const nums = s
.split("(")[1]
.split(")")[0]
.split(",")
.map((x) => Number(x));
const res = nums[0] * nums[1];
return acc + res;
}
return acc;
}, 0);
};
console.log(part1());
console.log(part2());