aboutsummaryrefslogtreecommitdiff
path: root/racket/aoc2023/day-02/day-02.rkt
diff options
context:
space:
mode:
Diffstat (limited to 'racket/aoc2023/day-02/day-02.rkt')
-rw-r--r--racket/aoc2023/day-02/day-02.rkt35
1 files changed, 35 insertions, 0 deletions
diff --git a/racket/aoc2023/day-02/day-02.rkt b/racket/aoc2023/day-02/day-02.rkt
new file mode 100644
index 0000000..973d20c
--- /dev/null
+++ b/racket/aoc2023/day-02/day-02.rkt
@@ -0,0 +1,35 @@
+#lang racket
+
+(require advent-of-code)
+
+(struct roll (id red green blue))
+
+(define all-games
+ (for/list ([raw-game (in-list (port->lines (open-aoc-input (find-session) 2023 2)))]
+ #:do [(define game (string-trim raw-game "Game "))
+ (match-define (list id trials) (string-split game ": "))])
+ (for/list ([trial (in-list (string-split trials "; "))])
+ (for/fold ([acc (roll (string->number id) 0 0 0)])
+ ([color (in-list (string-split trial ", "))])
+ (match (string-split color)
+ [(list (app string->number n) "red") (struct-copy roll acc [red n])]
+ [(list (app string->number n) "green") (struct-copy roll acc [green n])]
+ [(list (app string->number n) "blue") (struct-copy roll acc [blue n])])))))
+
+;; part 1
+(for/sum ([game (in-list all-games)]
+ #:when (andmap (λ (g) (and ((roll-red g) . <= . 12)
+ ((roll-green g) . <= . 13)
+ ((roll-blue g) . <= . 14)))
+ game))
+ (roll-id (first game)))
+
+;; part 2
+(for/sum ([game (in-list all-games)])
+ (define max-cubes
+ (for/fold ([acc (roll #f 0 0 0)]) ([r (in-list game)])
+ (roll #f
+ (max (roll-red acc) (roll-red r))
+ (max (roll-green acc) (roll-green r))
+ (max (roll-blue acc) (roll-blue r)))))
+ (* (roll-red max-cubes) (roll-green max-cubes) (roll-blue max-cubes)))