Output:
Advent of Code day 1 part 2
let filteredData = data.split(" ").filter(bit => bit !== null && bit !== "")
let tempVal = ""
let total = 0
for (let line of filteredData) {
line = line.replaceAll('one','o1ne')
line = line.replaceAll('two','t2wo')
line = line.replaceAll('three','t3hree')
line = line.replaceAll('four','f4our')
line = line.replaceAll('five','f5ive')
line = line.replaceAll('six','s6ix')
line = line.replaceAll('seven','s7even')
line = line.replaceAll('eight','e8ight')
line = line.replaceAll('nine','n9ine')
for (let char of line.split("")) {
if (/\d/.test(char)) {
tempVal += char
break
}
}
for (let char of line.split("").reverse()) {
if (/\d/.test(char)) {
tempVal += char
break
}
}
console.log(tempVal)
total += parseInt(tempVal, 10)
tempVal = ""
}
return total
Almost identical to my previous solution except I use 'replaceAll()' to make sure I pick up on numbers spelt with letters. I had to make sure I replaced 'nine' with 'n9ine' for example as a number could be 'nineight' and the eight would be the important character, but nine was changed etc.