Day 5 Part 2: Missing code

This commit is contained in:
Daniel Ziltener 2023-12-05 18:04:21 +01:00
parent 3a25264ff2
commit ae9172b274
Signed by: zilti
GPG Key ID: B38976E82C9DAE42
1 changed files with 17 additions and 14 deletions

View File

@ -2068,24 +2068,27 @@ almanac. *What is the lowest location number that corresponds to any of the init
*** Puzzle Solution
All that is needed is to "expand" the seed numbers before continuing:
All that is needed is to "expand" the seed numbers before continuing. Since we are dealing with such
a huge number of seeds, I use ~delay-force~ to delay calculation of the later ones until they're
actually needed.
#+NAME: day5-part2-expand-seeds
#+begin_src scheme :tangle day5.scm
(define (expand-seed start size)
(let ((max (fx- (fx+ start size) 1)))
(do ((seedlist (list start)
(cons (fx+ (car seedlist) 1)
seedlist)))
((fx< (fx- max 2) (car seedlist))
seedlist))))
(define (expand-seed existing-seeds start size)
(let ((max (- (+ start size) 1)))
(do ((seedlist (cons start existing-seeds)
(cons (+ (car seedlist) 1)
seedlist)))
((< (- max 2) (car seedlist))
seedlist))))
(define (expand-seeds seed-nums)
(if (fx<= 2 (length seed-nums))
(append
(expand-seed (car seed-nums) (cadr seed-nums))
(expand-seeds (cddr seed-nums)))
'()))
(define (expand-seeds seed-nums #!optional (existing-seeds '()))
(if (< 2 (length seed-nums))
(expand-seeds (cddr seed-nums)
(delay-force (expand-seed existing-seeds
(car seed-nums)
(cadr seed-nums))))
(expand-seed existing-seeds (car seed-nums) (cadr seed-nums))))
#+end_src
After that, it is /almost/ identical to part 1, but we have to replace the ~foldl~ with a recursive