33 lines
711 B
JavaScript
33 lines
711 B
JavaScript
import * as R from 'remeda';
|
|
|
|
import { extract_number } from './part1.js';
|
|
|
|
const spelled = {
|
|
'one' : 1,
|
|
'two' : 2,
|
|
'three' : 3,
|
|
'four' : 4,
|
|
'five' : 5,
|
|
'six' : 6,
|
|
'seven' : 7,
|
|
'eight' : 8,
|
|
'nine' : 9,
|
|
};
|
|
|
|
const resolve_spelled_numbers = (line) => {
|
|
const re = Object.keys(spelled).join('|') + '|\\d';
|
|
|
|
const first = line.match( RegExp( `^.*?(${re})`) )[1];
|
|
const last = line.match( RegExp( `^.*(${re})`) )[1];
|
|
|
|
return [ first, last ].map( n => spelled[n] ?? n ).join('');
|
|
}
|
|
|
|
export const solution_2 = R.createPipe(
|
|
i => i.split("\n"),
|
|
R.compact,
|
|
R.map(resolve_spelled_numbers),
|
|
R.map(extract_number),
|
|
R.sumBy(R.identity),
|
|
);
|