blob: 76cc24f09988817265b329cadd2a1e482c574712 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
#lang racket
(require racket/hash
advent-of-code
data/applicative
data/either
data/monad
megaparsack
megaparsack/text
threading)
(struct game (id r g b))
(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))))
(define draw/p
(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
(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))))
;; 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))
;; part 2
(for/sum ([m (in-list game-maxima)])
(* (game-r m) (game-g m) (game-b m)))
|