#lang curly-fn racket (require "scripts/aoc.rkt") ;; solution for day 4 (define (part1 input) (for/sum ([card (in-list input)]) (match-define (list winning have) card) (floor (expt 2 (sub1 (count #{member % have} winning)))))) (define (part2 input) (define score (make-hash)) (define total-score (make-hash)) (for ([card (in-list input)] [i (in-naturals)]) (match-define (list winning have) card) (define sc (count #{member % have} winning)) (hash-set! score (- (length input) i 1) sc) (hash-set! total-score (- (length input) i 1) 1)) (for ([i (in-range (length input))]) (define sc (hash-ref score i)) (define total-sc (for/sum ([j (in-range (- i sc) i)]) (hash-ref total-score j))) (hash-set! total-score i (+ (hash-ref total-score i) total-sc))) (apply + (hash-values total-score))) (define (parse fname) (for/list ([line (in-list (file->lines fname))]) (match-define (pregexp #px"^Card +[0-9]+: ([^\\|]+) \\| ([^\\|]+)$" (list _ left right)) line) (define winning (map string->number (string-split left))) (define have (map string->number (string-split right))) (list winning have))) (module+ main (define input (parse "inputs/4")) (answer 4 1 (time (part1 input))) (answer 4 2 (time (part2 input))) (displayln "meow"))