Files
advent-of-code/2023/day01/index.js
2024-12-16 13:11:17 +01:00

27 lines
859 B
JavaScript

import fs from 'node:fs'
const numberphiles = new Map([
["one", 1],
["two", 2],
["three", 3],
["four", 4],
["five", 5],
["six", 6],
["seven", 7],
["eight", 8],
["nine", 9]
])
fs.readFile('input.txt', ({ encoding: 'utf-8' }), (err, data) => {
const regex1 = /(\d)/gm
// let [first, last] = [matches[0], matches[matches.length-1]]
const regex = /(?=(one|two|three|four|five|six|seven|eight|nine|\d))/gm
let result = 0
for (let line of data.split('\n')) {
const matches = [...line.matchAll(regex)]
let [first, last] = [matches[0][1], matches[matches.length - 1][1]]
first = numberphiles.has(first) ? numberphiles.get(first) : first
last = numberphiles.has(last) ? numberphiles.get(last) : last
result += parseInt(`${first}${last} `)
}
console.log(result)
})