#lang curly-fn racket (require "scripts/aoc.rkt") ;; solution for day 8 (define (part1 input [cur "AAA"] [break? #{equal? % "ZZZ"}]) (for/fold ([cur cur] [cnt 0] #:result cnt) ([inst (in-cycle (in-list (first input)))]) #:break (break? cur) (values (inst (hash-ref (second input) cur)) (add1 cnt)))) (define (part2 input) (~> (filter #{string-suffix? % "A"} (hash-keys (second input))) (map #{part1 input % #{string-suffix? % "Z"}} _) (apply lcm _))) (define (parse fname) (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))))) (module+ main (define input (parse "inputs/8")) (answer 8 1 (time (part1 input))) (answer 8 2 (time (part2 input))) (displayln "meow"))