2023-12-08 05:20:58 +00:00
|
|
|
#lang curly-fn racket
|
|
|
|
|
|
|
|
(require "scripts/aoc.rkt")
|
|
|
|
|
|
|
|
;; solution for day 8
|
|
|
|
|
2023-12-08 05:36:01 +00:00
|
|
|
(define (part1 input [cur "AAA"] [break? #{equal? % "ZZZ"}])
|
|
|
|
(for/fold ([cur cur] [cnt 0] #:result cnt)
|
2023-12-08 05:20:58 +00:00
|
|
|
([inst (in-cycle (in-list (first input)))])
|
2023-12-08 05:36:01 +00:00
|
|
|
#:break (break? cur)
|
|
|
|
(values (inst (hash-ref (second input) cur))
|
|
|
|
(add1 cnt))))
|
2023-12-08 05:20:58 +00:00
|
|
|
|
|
|
|
(define (part2 input)
|
2023-12-08 05:36:01 +00:00
|
|
|
(~> (filter #{string-suffix? % "A"}
|
|
|
|
(hash-keys (second input)))
|
|
|
|
(map #{part1 input % #{string-suffix? % "Z"}} _)
|
|
|
|
(apply lcm _)))
|
2023-12-08 05:20:58 +00:00
|
|
|
|
|
|
|
(define (parse fname)
|
2023-12-08 05:36:01 +00:00
|
|
|
(match-define (list* instl _ mapl) (file->lines fname))
|
|
|
|
(list
|
|
|
|
(for/list ([c (in-string instl)])
|
|
|
|
(match c
|
|
|
|
[#\L first]
|
|
|
|
[#\R second]))
|
|
|
|
(for/hash ([line (in-list mapl)])
|
|
|
|
(match-define (list node left right)
|
|
|
|
(regexp-match* #px"[A-Z0-9]{3}" line))
|
|
|
|
(values node (list left right)))))
|
2023-12-08 05:20:58 +00:00
|
|
|
|
|
|
|
(module+ main
|
|
|
|
(define input (parse "inputs/8"))
|
|
|
|
(answer 8 1 (time (part1 input)))
|
|
|
|
(answer 8 2 (time (part2 input)))
|
|
|
|
(displayln "meow"))
|