Day 1Trebuchet?!
View puzzle description on Advent of CodePart 1 of this was relatively straight-forward, however part 2 added some complications.
My first solution to part 2 would replace all occurences of spelled out numbers (one, two, three, etc) with the numeric version using something like this.
str.replace(/(one|two|three|etc)/g, getDigit);
However this caused issues in cases where the string ended with a pair of spelled out numbers which shared a end/start character, for example twone, oneight. In these cases, the string would be read left-to-right, so the first number would be replaced, thus breaking the second number. So twone would become 2ne and oneight would become 1ight, which would result in an incorrect total.
I considered an approach where I would read the string backwards and replace occurences of reversed spelled out numbers, eg eno, owt, etc, however this felt messy and I was sure there was a better way. It turns out there's not really a nice way of doing it, and in the end I had to seek out a hint from the subreddit.
import { map, pipe, replace, split, sum } from 'rambda';const getFirstAndLastChar = (x: string): string => `${x.at(0)}${x.at(-1)}`;const removeLetters = replace(/[a-z]/gi, '');const splitLines = split('\n');export default pipe(removeLetters,splitLines,map(getFirstAndLastChar),map(Number),sum,);