Last active
December 1, 2023 21:02
-
-
Save jaredreich/bb7f21373bc05b350e0aabf2ef12fd69 to your computer and use it in GitHub Desktop.
AOC 2023
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
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); | |
} |
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
// todo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment