Skip to content

Instantly share code, notes, and snippets.

@dan-coulter
Created December 7, 2023 03:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dan-coulter/83ec474fc4536fd6178ede5acbe2c4a1 to your computer and use it in GitHub Desktop.
Save dan-coulter/83ec474fc4536fd6178ede5acbe2c4a1 to your computer and use it in GitHub Desktop.
Advent of Code 2023 #01
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