Skip to content

Instantly share code, notes, and snippets.

@jaredreich
Last active December 1, 2023 21:02
Show Gist options
  • Save jaredreich/bb7f21373bc05b350e0aabf2ef12fd69 to your computer and use it in GitHub Desktop.
Save jaredreich/bb7f21373bc05b350e0aabf2ef12fd69 to your computer and use it in GitHub Desktop.
AOC 2023
import fs from 'fs';
const input = fs.readFileSync('./01/input.txt', { encoding: 'utf8' });
const injectNumbers = (text, name, value) => {
while (text.search(name) > -1) {
const index = text.search(name);
text = [text.slice(0, index + 1), value, text.slice(index + 1)].join('');
}
return text;
}
const textMap = { one: '1', two: '2', three: '3', four: '4', five: '5', six: '6', seven: '7', eight: '8', nine: '9' };
export default async () => {
const inputArray = input.split('\n');
const inputArrayNumbersOnly = inputArray.map(text => {
Object.keys(textMap).forEach(key => {
const value = textMap[key];
text = injectNumbers(text, key, value);
});
const textNumbersArray = text.replace(/\D/g, '').split('');
const firstAndLastTextNumbers = `${textNumbersArray[0]}${textNumbersArray[textNumbersArray.length - 1]}`;
return Number(firstAndLastTextNumbers);
});
let sum = 0;
inputArrayNumbersOnly.forEach(num => sum += num);
console.log('Sum:', sum);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment