-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
45 lines (39 loc) · 1 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
import { isNumber } from 'lodash';
import { INPUT_PATH, runner, SAMPLE_PATH } from '../../../lib/utils';
const path = `${__dirname}/${INPUT_PATH}`;
const p1 = (lines: string) => {
return lines
.match(/(mul\()(\d+,\d+)\)/gm)
?.map((mul) =>
mul
.replace(/[mul()]+/g, '')
.split(',')
.map((num) => Number(num)),
)
.reduce((acc, [x, y]) => acc + x * y, 0);
};
const p2 = (lines: string) => {
let flag = true;
return lines
.match(/(mul\()(\d+,\d+)\)|(do\(\))|(don't\(\))/gm)
?.map((mul) => {
if (mul === 'do()') flag = true;
if (mul === "don't()") flag = false;
return flag
? mul
.replace(/[mul()]+/g, '')
.split(',')
.filter((num) => !isNaN(Number(num)))
.map((num) => Number(num))
: [];
})
.filter((el) => el?.length)
.reduce((acc, [x, y]) => acc + x * y, 0);
};
// Part 1
runner({
path,
solution: (input) => {
return { p1: p1(input), p2: p2(input) };
},
});