-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01_Trebuchet.js
More file actions
31 lines (24 loc) · 796 Bytes
/
Copy path01_Trebuchet.js
File metadata and controls
31 lines (24 loc) · 796 Bytes
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
import { textToArray, readInput } from '../lib/utils.js';
const list = textToArray(readInput());
const WORD_NUMS = "one_two_three_four_five_six_seven_eight_nine".split("_");
const solve = (inp, p2 = false) => {
return inp.map(x => {
const nums = [];
for (let i = 0; i < x.length; i++) {
if (x[i].match(/^\d$/)) {
nums.push(x[i]);
}
if (p2) {
for (let n = 0; n < WORD_NUMS.length; n++) {
if (x.startsWith(WORD_NUMS[n], i)) {
nums.push(`${n + 1}`);
break;
}
}
}
}
return parseInt(nums.at(0) + nums.at(-1));
}).reduce((a, b) => a + b);
}
console.log(solve(list) || "-") // don't error if run with second example input
console.log(solve(list, true))