aboutsummaryrefslogtreecommitdiff
path: root/aoc2023-other
diff options
context:
space:
mode:
authorJ.J <thechairman@thechairman.info>2023-12-02 02:08:18 -0500
committerJ.J <thechairman@thechairman.info>2023-12-02 02:08:18 -0500
commit9f4a95984997dc72736f2ea74723a7e7b5b35829 (patch)
tree0c643bd8b76d46dd4569ae71d25bc3a543de0003 /aoc2023-other
parent150a167812e63618af5860a3cd79b733402e3e9a (diff)
downloadgleam_aoc-9f4a95984997dc72736f2ea74723a7e7b5b35829.tar.gz
gleam_aoc-9f4a95984997dc72736f2ea74723a7e7b5b35829.zip
day 2 racket complete
Diffstat (limited to 'aoc2023-other')
-rw-r--r--aoc2023-other/day-02/day-02.rkt22
1 files changed, 11 insertions, 11 deletions
diff --git a/aoc2023-other/day-02/day-02.rkt b/aoc2023-other/day-02/day-02.rkt
index 20193b4..d37978e 100644
--- a/aoc2023-other/day-02/day-02.rkt
+++ b/aoc2023-other/day-02/day-02.rkt
@@ -2,33 +2,33 @@
(require advent-of-code)
-(struct roll (red green blue))
+(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 0 0 0)]) ([color (in-list (string-split trial ", "))])
+ (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 id) (in-indexed all-games)])
- (define id-or-nothing
- (for/and ([r (in-list game)])
- (if (and ((roll-red r) . <= . 12) ((roll-green r) . <= . 13) ((roll-blue r) . <= . 14))
- (add1 id)
- #f)))
- (if id-or-nothing id-or-nothing 0))
+(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 0 0 0)]) ([r (in-list game)])
- (roll (max (roll-red acc) (roll-red r))
+ (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)))