diff options
author | Hunky Jimpjorps <thechairman@thechairman.info> | 2023-12-08 09:42:13 -0500 |
---|---|---|
committer | Hunky Jimpjorps <thechairman@thechairman.info> | 2023-12-08 09:42:13 -0500 |
commit | df2f2fd140f188dfb5df68b44e397a8855e79f02 (patch) | |
tree | 9bb603eba8a3c46848bc59a6c5ba1c73ed34759c /aoc2023-other/day-08/day-08.rkt | |
parent | 82c3ecec3de5111b460910bafe141b3aed478676 (diff) | |
parent | b51dd4b3768bce0209733ef2562cb96e5330d3c6 (diff) | |
download | gleam_aoc-df2f2fd140f188dfb5df68b44e397a8855e79f02.tar.gz gleam_aoc-df2f2fd140f188dfb5df68b44e397a8855e79f02.zip |
Merge branch 'main' of https://github.com/hunkyjimpjorps/AdventOfCode
Diffstat (limited to 'aoc2023-other/day-08/day-08.rkt')
-rw-r--r-- | aoc2023-other/day-08/day-08.rkt | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/aoc2023-other/day-08/day-08.rkt b/aoc2023-other/day-08/day-08.rkt new file mode 100644 index 0000000..bc234b5 --- /dev/null +++ b/aoc2023-other/day-08/day-08.rkt @@ -0,0 +1,32 @@ +#lang racket + +(require advent-of-code + threading) + +(struct exits (left right) #:transparent) + +(match-define (list raw-directions raw-maze) + (~> (fetch-aoc-input (find-session) 2023 8 #:cache #true) (string-split "\n\n"))) + +(define directions (string->list raw-directions)) + +(define maze + (for/hash ([line (in-list (string-split raw-maze "\n"))]) + (match (regexp-match #rx"(...) = \\((...), (...)\\)" line) + [(list _ name left right) (values name (exits left right))]))) + +(define (to-next-node start end dirs maze) + (for/fold ([current start] [acc 0] #:result acc) ([dir (in-cycle dirs)]) + #:break (string-suffix? current end) + (define node (hash-ref maze current)) + (case dir + [(#\L) (values (exits-left node) (add1 acc))] + [(#\R) (values (exits-right node) (add1 acc))]))) + +;; part 1 +(to-next-node "AAA" "ZZZ" directions maze) + +;; part 2 +(for/lists (ns #:result (apply lcm ns)) + ([start (in-list (hash-keys maze))] #:when (string-suffix? start "A")) + (to-next-node start "Z" directions maze)) |