aboutsummaryrefslogtreecommitdiff
path: root/aoc2021/day-02/day-02.rkt
diff options
context:
space:
mode:
authorJ.J <thechairman@thechairman.info>2023-11-30 17:10:00 -0500
committerJ.J <thechairman@thechairman.info>2023-11-30 17:10:00 -0500
commit8ab65dc2da1742eb86ec636c50c7018385b68167 (patch)
treec4fd556aca9b867cfa1f2f174128c30857353884 /aoc2021/day-02/day-02.rkt
parentfafbeaf9e3c09ba7a5bea7e47d5736001f8a5aa1 (diff)
downloadgleam_aoc-8ab65dc2da1742eb86ec636c50c7018385b68167.tar.gz
gleam_aoc-8ab65dc2da1742eb86ec636c50c7018385b68167.zip
prep for 2023, renaming for consistency
Diffstat (limited to 'aoc2021/day-02/day-02.rkt')
-rw-r--r--aoc2021/day-02/day-02.rkt24
1 files changed, 24 insertions, 0 deletions
diff --git a/aoc2021/day-02/day-02.rkt b/aoc2021/day-02/day-02.rkt
new file mode 100644
index 0000000..0bd0c3d
--- /dev/null
+++ b/aoc2021/day-02/day-02.rkt
@@ -0,0 +1,24 @@
+#lang racket
+(require advent-of-code
+ threading
+ algorithms)
+
+(define motion-data
+ (~> (open-aoc-input (find-session) 2021 2 #:cache (string->path "./cache"))
+ (port->list read _)
+ (chunks-of _ 2)))
+
+;; part 1
+(for/fold ([depth 0] [position 0] #:result (* depth position)) ([motion (in-list motion-data)])
+ (match motion
+ [(list 'forward x) (values depth (+ position x))]
+ [(list 'up x) (values (- depth x) position)]
+ [(list 'down x) (values (+ depth x) position)]))
+
+;; part 2
+(for/fold ([aim 0] [depth 0] [position 0] #:result (* depth position))
+ ([motion (in-list motion-data)])
+ (match motion
+ [(list 'forward x) (values aim (+ depth (* aim x)) (+ position x))]
+ [(list 'up x) (values (- aim x) depth position)]
+ [(list 'down x) (values (+ aim x) depth position)]))