aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJ.J <thechairman@thechairman.info>2023-12-02 02:01:32 -0500
committerJ.J <thechairman@thechairman.info>2023-12-02 02:01:32 -0500
commit150a167812e63618af5860a3cd79b733402e3e9a (patch)
treec865d64f437ef50b10b80666a859cabc8306ea18
parent2e7a0fd7ce39296ec9056bc4edda45f8c28146f6 (diff)
downloadgleam_aoc-150a167812e63618af5860a3cd79b733402e3e9a.tar.gz
gleam_aoc-150a167812e63618af5860a3cd79b733402e3e9a.zip
day 2 racket solution
-rw-r--r--aoc2023-other/day-02/day-02.rkt34
-rw-r--r--aoc2023/README.md6
-rw-r--r--aoc2023/adglent_cheatsheet.md3
3 files changed, 40 insertions, 3 deletions
diff --git a/aoc2023-other/day-02/day-02.rkt b/aoc2023-other/day-02/day-02.rkt
new file mode 100644
index 0000000..20193b4
--- /dev/null
+++ b/aoc2023-other/day-02/day-02.rkt
@@ -0,0 +1,34 @@
+#lang racket
+
+(require advent-of-code)
+
+(struct roll (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 ", "))])
+ (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))
+
+;; 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))
+ (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)))
diff --git a/aoc2023/README.md b/aoc2023/README.md
index 921dfae..3f534e8 100644
--- a/aoc2023/README.md
+++ b/aoc2023/README.md
@@ -20,3 +20,9 @@ gleam add aoc2023
```
and its documentation can be found at <https://hexdocs.pm/aoc2023>.
+
+## Use
+
+* Set up a solution: `gleam run -m adglent/day <n>`
+* Check against examples: `gleam test -- --modules=day<n>/day<n>_test`
+* Get final answer: `gleam run -m day<n>/solve <p>` \ No newline at end of file
diff --git a/aoc2023/adglent_cheatsheet.md b/aoc2023/adglent_cheatsheet.md
deleted file mode 100644
index 4061e1b..0000000
--- a/aoc2023/adglent_cheatsheet.md
+++ /dev/null
@@ -1,3 +0,0 @@
-Set up a solution: `gleam run -m adglent/day <n>`
-Check against examples: `gleam test -- --modules=day<n>/day<n>_test`
-Get final answer: `gleam run -m day<n>/solve <p>` \ No newline at end of file