Created
December 3, 2023 00:15
-
-
Save justinline/b796e24003c89747f6fd25190ffffb49 to your computer and use it in GitHub Desktop.
Advent of code day 2
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 in browser console only | |
| // Saved text input as Global Variable to access temp1 | |
| const games = temp1.textContent.split('\n') | |
| const regex = /(\d+) (green|red|blue)/g | |
| const asObjects = games.map(game => [...game.matchAll(regex)].reduce((colors, matches) => { | |
| const [_, number, color] = matches | |
| if (number > colors[color]) return {...colors, [color]: Number(number)} | |
| return colors; | |
| }, { red: 0, green: 0, blue: 0})).flat().slice(0,100) // slice off the last entry which is blank | |
| const part1Result = asObjects.reduce((sum, game, index) => { | |
| if (game.red <= possible.red && game.green <= possible.green && game.blue <= possible.blue) return sum + index + 1 | |
| return sum | |
| }, 0) | |
| const part2Result = asObjects.reduce((sum, game, index) => { | |
| const powerOfAllValues = Object.values(game).reduce((s, v) => s * v, 1) | |
| return sum + powerOfAllValues | |
| }, 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment