-
Notifications
You must be signed in to change notification settings - Fork 0
/
one.ts
43 lines (36 loc) · 980 Bytes
/
one.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
import inputs from "./inputs";
const getInt = (reg: RegExpMatchArray | null) => {
if (!reg) return 0;
const str = reg[1];
let x = parseInt(str);
if (isNaN(x)) {
if (str === "one") x = 1;
if (str === "two") x = 2;
if (str === "three") x = 3;
if (str === "four") x = 4;
if (str === "five") x = 5;
if (str === "six") x = 6;
if (str === "seven") x = 7;
if (str === "eight") x = 8;
if (str === "nine") x = 9;
}
return x;
};
const res = inputs.one
.split("\n")
// .slice(0, 100)
.map((line) => {
const reg =
/(?=(one|two|three|four|five|six|seven|eight|nine|[123456789]))/g;
const arr = [...line.matchAll(reg)];
const first = arr[0];
const last = arr[arr.length - 1];
const a = getInt(first);
const b = getInt(last);
const val = a * 10 + b;
// console.log({ line, val });
return val;
});
const sum = res.reduce((tot, x) => tot + x, 0);
// console.log(res);
console.log({ sum });