aboutsummaryrefslogtreecommitdiff
path: root/aoc2016
diff options
context:
space:
mode:
authorHJ <thechairman@thechairman.info>2023-12-14 23:41:03 -0500
committerHJ <thechairman@thechairman.info>2023-12-14 23:41:03 -0500
commitfcd6998f07bb9f44dafcb029156ae171ee4b0387 (patch)
tree64eeb9683d0c995f3b443ebd295576da07ede2db /aoc2016
parent57955f552b2d80b5e1a8a06fb12df8c58ec42fe0 (diff)
downloadgleam_aoc-fcd6998f07bb9f44dafcb029156ae171ee4b0387.tar.gz
gleam_aoc-fcd6998f07bb9f44dafcb029156ae171ee4b0387.zip
2016 day 1 part 1
Diffstat (limited to 'aoc2016')
-rw-r--r--aoc2016/day-01/day-01.rkt38
1 files changed, 38 insertions, 0 deletions
diff --git a/aoc2016/day-01/day-01.rkt b/aoc2016/day-01/day-01.rkt
new file mode 100644
index 0000000..d18c819
--- /dev/null
+++ b/aoc2016/day-01/day-01.rkt
@@ -0,0 +1,38 @@
+#lang racket
+
+(require advent-of-code
+ threading)
+
+(struct posn (x y) #:transparent)
+(struct dir (x y) #:transparent)
+
+(define/match (left _d)
+ [((dir 0 1)) (dir -1 0)]
+ [((dir -1 0)) (dir 0 -1)]
+ [((dir 0 -1)) (dir 1 0)]
+ [((dir 1 0)) (dir 0 1)])
+
+(define/match (right _d)
+ [((dir 0 1)) (dir 1 0)]
+ [((dir 1 0)) (dir 0 -1)]
+ [((dir 0 -1)) (dir -1 0)]
+ [((dir -1 0)) (dir 0 1)])
+
+(define/match (go _from _d _dist)
+ [((posn x y) (dir dx dy) dist) (posn (+ x (* dx dist)) (+ y (* dy dist)))])
+
+(define input (~> (fetch-aoc-input (find-session) 2016 1 #:cache #true) string-trim (string-split ", ")))
+
+;; part 1
+(for/fold ([p (posn 0 0)]
+ [d (dir 0 1)]
+ #:result (+ (abs (posn-x p)) (abs (posn-y p))))
+ ([instruction (in-list input)])
+ (displayln (~a p " " d))
+ (match instruction
+ [(regexp #rx"L(.*)" (list _ n)) (values (go p (left d) (string->number n))
+ (left d))]
+ [(regexp #rx"R(.*)" (list _ n)) (values (go p (right d) (string->number n))
+ (right d))]))
+
+;; part 2