Created
December 7, 2023 03:09
-
-
Save dan-coulter/83ec474fc4536fd6178ede5acbe2c4a1 to your computer and use it in GitHub Desktop.
Advent of Code 2023 #01
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { readFile } = require('node:fs/promises'); | |
const filename = 'input.txt'; | |
const firstDigitRegex = /\d/; | |
const lastDigitRegex = /\d[a-z]*$/; | |
const parseCalibrationFile = async (file) => { | |
const data = (await readFile('input.txt', 'utf8')).split('\n'); | |
const sum = data.reduce((acc, line) => { | |
const firstDigit = parseInt(line.match(firstDigitRegex) ?? '0'); | |
const lastDigit = parseInt(line.match(lastDigitRegex) ?? '0'); | |
return acc + firstDigit * 10 + lastDigit; | |
}, 0); | |
return sum; | |
}; | |
parseCalibrationFile(filename).then(console.log); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment