diff options
author | J.J <thechairman@thechairman.info> | 2023-12-03 12:20:36 -0500 |
---|---|---|
committer | J.J <thechairman@thechairman.info> | 2023-12-03 12:20:36 -0500 |
commit | 03a8465fef75336989ff7d20050b643e541e5a36 (patch) | |
tree | e30d983f8a532d86de22a08fd2f3fb77bc66bfd8 /aoc2023-other/day-02/day-02-parser.rkt | |
parent | 0607a032238b545d3d49d3af0e1763f82ccdd31a (diff) | |
parent | 8a521c9df766b733f03d357340f74935c65f7de7 (diff) | |
download | gleam_aoc-03a8465fef75336989ff7d20050b643e541e5a36.tar.gz gleam_aoc-03a8465fef75336989ff7d20050b643e541e5a36.zip |
Merge branch 'main' of https://github.com/hunkyjimpjorps/AdventOfCode
Diffstat (limited to 'aoc2023-other/day-02/day-02-parser.rkt')
-rw-r--r-- | aoc2023-other/day-02/day-02-parser.rkt | 44 |
1 files changed, 27 insertions, 17 deletions
diff --git a/aoc2023-other/day-02/day-02-parser.rkt b/aoc2023-other/day-02/day-02-parser.rkt index 67f5ae6..76cc24f 100644 --- a/aoc2023-other/day-02/day-02-parser.rkt +++ b/aoc2023-other/day-02/day-02-parser.rkt @@ -13,33 +13,43 @@ (define cube/p (do [n <- integer/p] - space/p - [c <- (or/p (string/p "red") (string/p "blue") (string/p "green"))] - (pure (cons c n)))) + space/p + [c <- (or/p (string/p "red") + (string/p "blue") + (string/p "green"))] + (pure (cons c n)))) (define draw/p - (do [xs <- (many/p cube/p #:min 1 #:max 3 #:sep (string/p ", "))] (pure (apply hash (flatten xs))))) + (do [xs <- (many/p cube/p #:min 1 #:max 3 #:sep (string/p ", "))] + (pure (apply hash (flatten xs))))) (define all-draws/p (do (string/p "Game ") - [id <- integer/p] - (string/p ": ") - [all-draws <- (many/p draw/p #:min 1 #:sep (string/p "; "))] - (define maxima (max-cubes all-draws)) - (pure (game id (hash-ref maxima "red") (hash-ref maxima "green") (hash-ref maxima "blue"))))) - -(define (max-cubes h) - (foldl (curry hash-union #:combine max) (hash "red" 0 "green" 0 "blue" 0) h)) + [id <- integer/p] + (string/p ": ") + [all-draws <- (many/p draw/p #:min 1 #:sep (string/p "; "))] + (define maxima + (foldl (curry hash-union #:combine max) + (hash "red" 0 "green" 0 "blue" 0) + all-draws)) + (pure (game id + (hash-ref maxima "red") + (hash-ref maxima "green") + (hash-ref maxima "blue"))))) (define game-maxima (~>> (open-aoc-input (find-session) 2023 2) port->lines - (map (λ~>> (parse-string all-draws/p) from-either)))) + (map (λ~>> (parse-string all-draws/p) + from-either)))) ;; part 1 -(for/sum ([m (in-list game-maxima)] #:unless - (or (> (game-r m) 12) (> (game-g m) 13) (> (game-b m) 14))) - (game-id m)) +(for/sum ([m (in-list game-maxima)] + #:unless (or (> (game-r m) 12) + (> (game-g m) 13) + (> (game-b m) 14))) + (game-id m)) ;; part 2 -(for/sum ([m (in-list game-maxima)]) (* (game-r m) (game-g m) (game-b m))) +(for/sum ([m (in-list game-maxima)]) + (* (game-r m) (game-g m) (game-b m))) |