Created
December 3, 2023 00:24
-
-
Save justinline/61024b3fbce091748d8b4e48bb80ddf5 to your computer and use it in GitHub Desktop.
Advent of Code day 1
This file contains hidden or 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
| // Done purely in the browser console, using temp1 which is the input text as a Global Variable | |
| // Might not give actual results, I started creating gists on day 2 and mostly recovered this from console history! 😆 | |
| const text = () => temp1.textContent.split('\n') | |
| const getFirstAndLastInteger = (str) => { | |
| const integers = str.split('').map(s => Number(s)).filter(Boolean) | |
| return Number(`${integers[0]}${integers[integers.length - 1]}`) | |
| } | |
| const regex = /(one|two|three|four|five|six|seven|eight|nine)/ | |
| const wordMap = { | |
| 'one': 'o1e', | |
| 'two': 't2o', | |
| 'three': 't3e', | |
| 'four': 'f4r', | |
| 'five': 'f5e', | |
| 'six': 's6x', | |
| 'seven': 's7n', | |
| 'eight': 'e8t', | |
| 'nine': 'n9n' | |
| } | |
| const parsed = (source) => { | |
| let last = source | |
| while (true) { | |
| if (last.match(regex) === null) return last; | |
| const result = last.replace(regex, (match) => wordMap[match]) | |
| last = result | |
| } | |
| } | |
| const part1Results = text().slice(0, text().length - 1).reduce((total, currentInstructions) => { | |
| const toAdd = getFirstAndLastInteger(currentInstructions) | |
| return total + toAdd | |
| }, 0) | |
| const part2Results = text().slice(0, text().length -1).reduce((total, currentInstructions) => { | |
| const correctedString = parsed(currentInstructions) | |
| const toAdd = getFirstAndLastInteger(correctedString) | |
| return total + toAdd | |
| }, 0) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment