-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01.ts
73 lines (64 loc) · 1.7 KB
/
01.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
67
68
69
70
71
72
73
import Task, { TaskPartSolution } from "../utils/task.js";
const part1: TaskPartSolution = (input) => {
const sum = input
.split("\n")
.map((row) => {
const clean = row.replaceAll(/[^0-9]/g, "");
const n = Number(`${clean[0]}${clean[clean.length - 1]}`);
return n;
})
.reduce((acc, v) => acc + v, 0);
return sum;
};
const NUMBERS = ["____zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"];
const part2: TaskPartSolution = (input) => {
const replaceNums = (str: string) => {
let res = str;
const first = NUMBERS.map((word, n) => ({ idx: str.indexOf(word), n, word }))
.filter(({ idx }) => idx != -1)
.sort((a, b) => a.idx - b.idx)[0];
if (first) {
const a = res.split("");
a.splice(first.idx, 0, first.n.toString());
res = a.join("");
}
const last = NUMBERS.map((word, n) => ({ idx: res.lastIndexOf(word), n, word }))
.filter(({ idx }) => idx != -1)
.sort((a, b) => b.idx - a.idx)[0];
if (last) {
const a = res.split("");
a.splice(last.idx, 0, last.n.toString());
res = a.join("");
}
return res;
};
const sum = input
.split("\n")
.map((row) => {
const clean = replaceNums(row).replaceAll(/[^0-9]/g, "");
const n = Number(`${clean[0]}${clean[clean.length - 1]}`);
return n;
})
.reduce((acc, v) => acc + v, 0);
return sum;
};
const task = new Task(2023, 1, part1, part2, {
part1: {
input: `1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet`,
result: "142",
},
part2: {
input: `two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen`,
result: "281",
},
});
export default task;