Day 5: If You Give A Seed A Fertilizer

This commit is contained in:
Daniel Ziltener 2023-12-05 20:47:02 +01:00
parent ae9172b274
commit f090f81d3f
Signed by: zilti
GPG Key ID: B38976E82C9DAE42
1 changed files with 24 additions and 1 deletions

View File

@ -1872,6 +1872,7 @@ So, the lowest location number in this example is *=35=*.
#+begin_src scheme :exports none :noweb yes :tangle day5.scm
(import (chicken fixnum)
(chicken sort)
(chicken string)
(chicken keyword)
(chicken irregex))
@ -2131,17 +2132,39 @@ function that handles the promises.
<<day5-part1-map-entity-forward>>
<<day5-part1-map-entity-forward-fully>>
<<day5-part2-expand-seeds>>
<<day5-part2-fold-seeds>>
<<day5-part2-calc>>
#+end_src
#+RESULTS: day5-part2-calc-full
: 1181555926
: 37806486
#+begin_src scheme :noweb yes :exports none :tangle day5.scm
(define (calc-part-2)
<<day5-part2-calc>>)
#+end_src
*** The Optimized Solution
For the optimized solution using ranges, a new record type is needed: ~ranged-entity~.
#+begin_src scheme :tangle day5.scm
(define-record ranged-entity type from-id to-id)
#+end_src
To map the ~ranged-entity~ onto the next step, it has to be split up according to the
~mapping-entry~ ranges.
#+begin_src scheme :tangle day5.scm
(define (map-ranged-entity-forward ranged-entity mapping-alist)
(let* ((mapping-entries (alist-ref (ranged-entity-type ranged-entity) mapping-alist))
(mapping-entries-alist (map (lambda (x) (cons (mapping-entry-from-start x)
x))
mapping-entries))
(sorted-range-starts (sort (map mapping-entry-from-start mapping-entries))))
))
#+end_src
** Puzzle Input
#+NAME: day5-input