diff options
author | J.J <thechairman@thechairman.info> | 2023-11-30 17:10:00 -0500 |
---|---|---|
committer | J.J <thechairman@thechairman.info> | 2023-11-30 17:10:00 -0500 |
commit | 8ab65dc2da1742eb86ec636c50c7018385b68167 (patch) | |
tree | c4fd556aca9b867cfa1f2f174128c30857353884 /aoc2022/day-05/day-05.rkt | |
parent | fafbeaf9e3c09ba7a5bea7e47d5736001f8a5aa1 (diff) | |
download | gleam_aoc-8ab65dc2da1742eb86ec636c50c7018385b68167.tar.gz gleam_aoc-8ab65dc2da1742eb86ec636c50c7018385b68167.zip |
prep for 2023, renaming for consistency
Diffstat (limited to 'aoc2022/day-05/day-05.rkt')
-rw-r--r-- | aoc2022/day-05/day-05.rkt | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/aoc2022/day-05/day-05.rkt b/aoc2022/day-05/day-05.rkt new file mode 100644 index 0000000..76d4ca6 --- /dev/null +++ b/aoc2022/day-05/day-05.rkt @@ -0,0 +1,46 @@ +#lang racket + +(require advent-of-code + threading + (only-in relation ->string ->list ->number) + (only-in algorithms chunks-of)) + +(define data (~> (fetch-aoc-input (find-session) 2022 5) (string-split "\n"))) + +(struct instruction (n from to)) + +(define crates-list + (~>> data + (take _ 8) + (map (λ~>> ->list)) + (apply map list _) + rest + (chunks-of _ 4) + (map (λ~> first ->string string-trim ->list) _))) + +(define crates + (for/hash ([c (in-list crates-list)] [i (in-naturals 1)]) + (values i c))) + +(define (parse-instruction str) + (match str + [(regexp #px"move (\\d+) from (\\d) to (\\d)" (list _ n from to)) + (instruction (->number n) (->number from) (->number to))])) + +(define instructions (~>> data (drop _ 10) (map parse-instruction))) + +(define (find-crate-message cs [reverse-function reverse]) + (for/fold ([current-crates cs] + #:result (~>> (hash-values current-crates) (map first) (apply string))) + ([i (in-list instructions)]) + (match-define (instruction n from to) i) + (define taken (~> (hash-ref current-crates from) (take _ n) reverse-function)) + (~> current-crates + (hash-update _ from (λ (v) (drop v n))) + (hash-update _ to (λ (v) (append taken v)))))) + +;; part 1 +(find-crate-message crates) + +;; part 2 +(find-crate-message crates identity) |