-
Notifications
You must be signed in to change notification settings - Fork 0
/
day3.ts
50 lines (44 loc) · 1.01 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
const input = await Deno.readTextFile("day3.input");
function processLine(line: string) {
let sum = 0;
const values = line.matchAll(/mul\(([0-9]{1,3}),([0-9]{1,3})\)/g);
for (const value of values) {
sum += parseInt(value[1]) * parseInt(value[2]);
}
return sum;
}
function part1() {
const lines = input.split("\n");
let sum = 0;
for (const line of lines) {
sum += processLine(line);
}
console.log(sum);
}
part1();
let process = true;
function processLine2(line: string) {
let sum = 0;
const values = line.matchAll(
/mul\(([0-9]{1,3}),([0-9]{1,3})\)|do\(\)|don't\(\)/g
);
for (const value of values) {
if (value[0] === "do()") {
process = true;
} else if (value[0] === "don't()") {
process = false;
} else if (process) {
sum += parseInt(value[1]) * parseInt(value[2]);
}
}
return sum;
}
function part2() {
const lines = input.split("\n");
let sum = 0;
for (const line of lines) {
sum += processLine2(line);
}
console.log(sum);
}
part2();