Day 4: Scratchcards
This commit is contained in:
parent
510ab99c4e
commit
de97d82354
|
@ -1 +1 @@
|
|||
((scheme-mode . ((geiser-scheme . 'chicken))))
|
||||
((nil . ((geiser-scheme-implementation . 'chicken))))
|
||||
|
|
80
chicken.org
80
chicken.org
|
@ -1,7 +1,7 @@
|
|||
# -*- geiser-scheme-implementation: chicken -*-
|
||||
#+TITLE: Advent Of Code with Chicken Scheme
|
||||
#+AUTHOR: Daniel Ziltener
|
||||
#+PROPERTY: header-args:scheme :session *chicken*
|
||||
#+PROPERTY: header-args:scheme :session *chicken* :comments both
|
||||
|
||||
* Prelude
|
||||
|
||||
|
@ -2122,9 +2122,11 @@ card's id (it might be useful in part 2), the winning numbers, and the card's nu
|
|||
|
||||
Since it could potentially be relevant, I also add a slot for the matches.
|
||||
|
||||
/UPDATE: I also added a slot for the number of copies of the scratch card - relevant for part 2./
|
||||
|
||||
#+NAME: day4-part1-scratchcard
|
||||
#+begin_src scheme :tangle day4.scm
|
||||
(define-record scratchcard id winning-numbers card-numbers match-numbers)
|
||||
(define-record scratchcard id winning-numbers card-numbers match-numbers copies)
|
||||
#+end_src
|
||||
|
||||
**** Parsing The Input
|
||||
|
@ -2164,7 +2166,7 @@ statement.
|
|||
(string-split (irregex-match-substring match 'winning-numbers-str)))
|
||||
(map string->number
|
||||
(string-split (irregex-match-substring match 'card-numbers-str)))
|
||||
'())
|
||||
'() 1)
|
||||
seed))
|
||||
'() input-str))
|
||||
#+end_src
|
||||
|
@ -2179,13 +2181,10 @@ name ends with an ~!~.
|
|||
#+NAME: day4-part1-card-matching
|
||||
#+begin_src scheme :tangle day4.scm
|
||||
(define (calc-card-matches card)
|
||||
(print "calculating matches for " (scratchcard-id card))
|
||||
(let ((winning-nums (scratchcard-winning-numbers card))
|
||||
(card-nums (scratchcard-card-numbers card)))
|
||||
(for-each (lambda (card-num)
|
||||
(print "\tCard number: " card-num)
|
||||
(when (foldl (lambda (matches? winning-num)
|
||||
(print "\t\tWinning number: " winning-num)
|
||||
(or matches?
|
||||
(= card-num winning-num)))
|
||||
#f winning-nums)
|
||||
|
@ -2292,7 +2291,74 @@ the original set of scratchcards, *how many total scratchcards do you end up wit
|
|||
|
||||
*** Puzzle Solution
|
||||
|
||||
WIP...
|
||||
#+NAME: day4-part2-card-alist
|
||||
#+begin_src scheme :tangle day4.scm
|
||||
(define (card-alist cards)
|
||||
(foldl (lambda (alist card)
|
||||
(calc-card-matches card)
|
||||
(alist-update (scratchcard-id card)
|
||||
card
|
||||
alist))
|
||||
'() (reverse cards)))
|
||||
#+end_src
|
||||
|
||||
#+NAME: day4-part2-gen-copies
|
||||
#+begin_src scheme :tangle day4.scm
|
||||
(define (generate-copies cardlist #!optional (index 1))
|
||||
(let ((index-card (alist-ref index cardlist)))
|
||||
(if index-card
|
||||
(let* ((matches (scratchcard-match-numbers index-card))
|
||||
(copies (scratchcard-copies index-card))
|
||||
(copy-indexes (foldl (lambda (indexes match)
|
||||
(cons (+ 1 index (length indexes))
|
||||
indexes))
|
||||
(list) matches)))
|
||||
(for-each (lambda (card-id)
|
||||
(let ((target-card (alist-ref card-id cardlist)))
|
||||
(scratchcard-copies-set!
|
||||
target-card
|
||||
(+ (scratchcard-copies target-card) copies))))
|
||||
copy-indexes)
|
||||
(generate-copies
|
||||
cardlist
|
||||
(+ index 1)))
|
||||
cardlist)))
|
||||
#+end_src
|
||||
|
||||
#+NAME: day4-part2-count-scratchcards
|
||||
#+begin_src scheme :tangle day4.scm
|
||||
(define (count-scratchcards cardlist)
|
||||
(foldl + 0 (map (compose scratchcard-copies cdr) cardlist)))
|
||||
#+end_src
|
||||
|
||||
#+NAME: day4-part2-calc
|
||||
#+begin_src scheme
|
||||
(let ((cardlist (card-alist (input->cards input))))
|
||||
(generate-copies cardlist)
|
||||
(count-scratchcards cardlist))
|
||||
#+end_src
|
||||
|
||||
#+NAME: day4-part2-calc-full
|
||||
#+begin_src scheme :var input=day4-input :noweb yes :exports results
|
||||
(import (chicken string)
|
||||
(chicken irregex))
|
||||
<<day4-part1-scratchcard>>
|
||||
<<day4-part1-card-irregex>>
|
||||
<<day4-part1-card-fold>>
|
||||
<<day4-part1-card-matching>>
|
||||
<<day4-part2-card-alist>>
|
||||
<<day4-part2-gen-copies>>
|
||||
<<day4-part2-count-scratchcards>>
|
||||
<<day4-part2-calc>>
|
||||
#+end_src
|
||||
|
||||
#+RESULTS: day4-part2-calc-full
|
||||
: 5923918
|
||||
|
||||
#+begin_src scheme :noweb yes :exports none :tangle day4.scm
|
||||
(define (calc-part-2)
|
||||
<<day4-part2-calc>>)
|
||||
#+end_src
|
||||
|
||||
** Puzzle Input
|
||||
|
||||
|
|
Loading…
Reference in New Issue