diff options
Diffstat (limited to 'racket/aoc2021/day-04')
-rw-r--r-- | racket/aoc2021/day-04/day-04.rkt | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/racket/aoc2021/day-04/day-04.rkt b/racket/aoc2021/day-04/day-04.rkt new file mode 100644 index 0000000..c572f74 --- /dev/null +++ b/racket/aoc2021/day-04/day-04.rkt @@ -0,0 +1,51 @@ +#lang racket +(require advent-of-code + threading + (only-in algorithms chunks-of)) + +(define data + (for/list ([l (in-lines (open-aoc-input (find-session) 2021 4 #:cache (string->path "./cache")))] + #:unless (equal? l "")) + l)) + +(define call-sheet (~> data car (string-split ",") (map string->number _))) +(define bingo-cards + (~> data cdr (map string-split _) (map (λ (row) (map string->number row)) _) (chunks-of 5))) + +(define test-card (first bingo-cards)) + +(define (mark-card card call) + (for/list ([row (in-list card)]) + (for/list ([col (in-list row)]) + (if (eq? col call) 'X col)))) + +(define (check-card card) + (for/or ([row (in-sequences card (apply map list card))]) + (equal? row '(X X X X X)))) + +(define (multiply-by-last-call n call) + (match n + ['X 0] + [n (* n call)])) + +(define (evaluate-cards cards calls [check (curry ormap check-card)] [exception not]) + (for/fold ([current-cards cards] + [last-call 0] + #:result (~> current-cards + (findf check-card _) + flatten + (map (λ (n) (multiply-by-last-call n last-call)) _) + (apply + _))) + ([call (in-list calls)]) + #:break (check current-cards) + (values (for/list ([card (in-list current-cards)] #:unless (exception card)) + (mark-card card call)) + call))) + +;; part 1 +(evaluate-cards bingo-cards call-sheet) +;; part 2 +(evaluate-cards bingo-cards + call-sheet + (λ (cards) (and (= (length cards) 1) (check-card (first cards)))) + check-card) |