-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathpart-two.js
49 lines (40 loc) · 953 Bytes
/
part-two.js
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
const { input } = require('./input');
const NUM_WORDS = {
one: 1,
two: 2,
three: 3,
four: 4,
five: 5,
six: 6,
seven: 7,
eight: 8,
nine: 9,
};
const NUM_WORD_ENTRIES = Object.entries(NUM_WORDS);
function strToNum(str) {
for (let [word, num] of NUM_WORD_ENTRIES) {
if (str.startsWith(word)) {
return num;
}
}
// Will get filtered out
return '';
}
const numbersInLines = input.map((line) => {
const allNumbers = line
.split('')
.map((char, i) => {
const restOfLineFromChar = line.slice(i);
// If we have a digit, return that. Otherwise, look to see if this char could be the start of a number word
return /\d/.test(char) ? char : strToNum(restOfLineFromChar);
})
.filter(Boolean);
const firstNumber = allNumbers[0];
const lastNumber = allNumbers[allNumbers.length - 1];
return parseInt(`${firstNumber}${lastNumber}`, 10);
});
let sum = 0;
for (let num of numbersInLines) {
sum += num;
}
console.log(sum);