Cube Conundrum Part 2

This commit is contained in:
Daniel Ziltener 2023-12-02 12:37:44 +01:00
parent 41b82a6a1e
commit 5d91cc2bf1
Signed by: zilti
GPG Key ID: B38976E82C9DAE42
1 changed files with 84 additions and 3 deletions

View File

@ -177,7 +177,6 @@ these together produces =281=.
#+RESULTS: day1-solution-2
: 53866
** Puzzle Input
Jump to [[#headline-10][day 2]].
@ -1350,7 +1349,7 @@ basically, it is nothing more than a recursive ~and~ statement.
And now, everything can be put together:
#+NAME: day2-part1-solution
#+begin_src scheme :noweb no-export
#+begin_src scheme :noweb yes :var input=day2-input
(import (chicken string)
(chicken keyword)
(chicken irregex))
@ -1367,7 +1366,89 @@ basically, it is nothing more than a recursive ~and~ statement.
** Part Two
To be done...
*** Quest
The Elf says they've stopped producing snow because they aren't getting any *water*! He isn't sure
why the water stopped; however, he can show you how to get to the water source to check it out for
yourself. It's just up ahead!
As you continue your walk, the Elf poses a second question: in each game you played, what is the
*fewest number of cubes of each color* that could have been in the bag to make the game possible?
Again consider the example games from earlier:
#+begin_example
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green
#+end_example
- In game 1, the game could have been played with as few as 4 red, 2 green, and 6 blue cubes. If any
color had even one fewer cube, the game would have been impossible.
- Game 2 could have been played with a minimum of 1 red, 3 green, and 4 blue cubes.
- Game 3 must have been played with at least 20 red, 13 green, and 6 blue cubes.
- Game 4 required at least 14 red, 3 green, and 15 blue cubes.
- Game 5 needed no fewer than 6 red, 3 green, and 2 blue cubes in the bag.
The *power* of a set of cubes is equal to the numbers of red, green, and blue cubes multiplied
together. The power of the minimum set of cubes in game 1 is =48=. In games 2-5 it was =12=, =1560=,
=630=, and =36=, respectively. Adding up these five powers produces the sum *=2286=*.
For each game, find the minimum set of cubes that must have been present. *What is the sum of the
power of these sets?*
*** Puzzle Solution
Most code can be reused from the first part; only the success check and the main function have to be
rewritten to accommodate the new requirements.
The success check gets replaced by a function that multiplies the minima together.
#+NAME: day2-part2-powercalc
#+begin_src scheme
(define (powercalc draws)
(* (apply max (map (lambda (draw)
(alist-ref #:red draw eqv? 1))
draws))
(apply max (map (lambda (draw)
(alist-ref #:green draw eqv? 1))
draws))
(apply max (map (lambda (draw)
(alist-ref #:blue draw eqv? 1))
draws))))
#+end_src
And the main function gets modified to sum everything up.
#+NAME: day2-part2-main-function
#+begin_src scheme
(define (game-set-power input)
(let ((games (record-fold input)))
(foldl + 0
(map (lambda (game)
(powercalc (cdr game)))
games))))
#+end_src
The full thing put together:
#+NAME: day2-part2-solution
#+begin_src scheme :noweb yes :var input=day2-input
(import (chicken string)
(chicken keyword)
(chicken irregex))
<<day2-part1-draw-processing>>
<<day2-part1-draw-splitting>>
<<day2-part1-record-splitting>>
<<day2-part2-powercalc>>
<<day2-part2-main-function>>
#+end_src
#+RESULTS: day2-part1-solution
: 78375
** Puzzle Input