-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday1.js
45 lines (36 loc) · 1.1 KB
/
day1.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
import { asLines } from './util.js';
const lineToCalibrationValueA = (line) => {
const first = line.match(/^[^\d]*(\d)/)[1];
const last = line.match(/(\d)[^\d]*$/)[1];
return parseInt(first + last);
};
const day1a = asLines('../input/day1_a.txt')
.map(lineToCalibrationValueA)
.reduce((a,b) => a + b, 0);
console.log(`day1 part a: ${day1a}`);
const lineToCalibrationValueB = (line) => {
return parseInt(findDigits(line).join(''));
}
const numbers = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
const number = (str) => {
if (str.charAt(0) > '0' && str.charAt(0) <= '9') {
return parseInt(str.charAt(0));
}
const i = numbers.findIndex((n) => str.startsWith(n));
return i < 0 ? null : i + 1;
};
const findDigits = (line) => {
let first, last;
for (let i = 0; i < line.length; i++) {
const n = number(line.substring(i));
if (n) {
first = first || n;
last = n;
}
}
return [first, last];
};
const day1b = asLines('../input/day1_a.txt')
.map(lineToCalibrationValueB)
.reduce((a,b) => a + b, 0);
console.log(`day1 part b: ${day1b}`);