AoC'21, day 4, second problem rvc


Return the last winning card’s score.

 $ cat 04b.rkt  
 #lang racket 
  
  
 (require 
   (prefix-in aoc04: "04a.rkt")) 
  
  
 (define (aoc04b . args) 
  
   ; Return the score of the last winning board in the given bingo 
   ; game.  If no game is given, use the contest problem input; 
   ; otherwise assume the argument is the string representation of 
   ; a problem input. 
  
   (run-the-game (apply aoc04:read-input args))) 
  
  
 (define (run-the-game bingo-game) 
  
   ; Return the last winning card's score in the given bingo 
   ; game. 
    
   (let* 
  
     ((last-winning-score 0) 
  
      (clear-winners (lambda (c bs) 
  
        ; Mark the given call on the given bingo card.  For 
        ; each winning card record its score and remove it  
        ; from the card list.  Return the remaining cards. 
  
        (filter 
  
          (lambda (b) 
            (let ((r (b c))) 
              (if (zero? r) 
                #t 
                (begin 
                  (set! last-winning-score (* r c)) 
                  #f)))) 
  
          bs)))) 
  
     (let loop 
  
       ((calls (car bingo-game)) 
        (boards (cdr bingo-game))) 
  
       (if (null? boards) 
         last-winning-score 
         (loop (cdr calls) (clear-winners (car calls) boards)))))) 
  
  
 (module+ main 
  
   (aoc04b)) 
  
  
 (define example-result 1924) 
  
  
 (module+ test 
  
   (require rackunit) 
  
   (check-equal? (aoc04b aoc04:example-data) example-result) 
   (check-equal? (aoc04b) 13158)) 
  
  
 $ raco test 04b.rkt  
 raco test: (submod "04b.rkt" test) 
 2 tests passed 
  
 $