diff options
author | Leah Neukirchen <leah@vuxu.org> | 2022-12-06 14:23:44 +0100 |
---|---|---|
committer | Leah Neukirchen <leah@vuxu.org> | 2022-12-06 14:23:44 +0100 |
commit | 0ffe55a0d6d5dbcff5cbc19507b336366f7fb40c (patch) | |
tree | 58fca570328f59ca08dc92b3cea90dcf8eacc730 /day05.rkt | |
parent | 68ee54cfaa49f438bb7ad93e7c595e72c64dfbbd (diff) | |
download | adventofcode2022-0ffe55a0d6d5dbcff5cbc19507b336366f7fb40c.tar.gz adventofcode2022-0ffe55a0d6d5dbcff5cbc19507b336366f7fb40c.tar.xz adventofcode2022-0ffe55a0d6d5dbcff5cbc19507b336366f7fb40c.zip |
day05
Diffstat (limited to 'day05.rkt')
-rw-r--r-- | day05.rkt | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/day05.rkt b/day05.rkt new file mode 100644 index 0000000..76d6350 --- /dev/null +++ b/day05.rkt @@ -0,0 +1,29 @@ +#lang racket + +(let-values ([(stacks steps) (splitf-at (file->lines "day05") + (lambda (s) (not (equal? s ""))))]) + + (let* ([state (for/vector ([x (in-range 1 35 4)]) + (dropf (for/list ([y (in-range 0 (sub1 (length stacks)))]) + (string-ref (list-ref stacks y) x)) + (lambda (c) (equal? c #\space))))] + [steps (for/list ([line (cdr steps)]) + (remove* '(#f) (map string->number (string-split line " "))))] + [solve (lambda (state f) + (for ([step steps]) + (let* ([n (first step)] + [from (sub1 (second step))] + [to (sub1 (third step))] + + [taken (take (vector-ref state from) n)] + [left (drop (vector-ref state from) n)] + [placed (append (f taken) (vector-ref state to))]) + (vector-set! state to placed) + (vector-set! state from left))) + (list->string (for/list ([s state]) + (first s))))]) + (displayln (solve (vector-copy state) reverse)) + (displayln (solve (vector-copy state) identity)) + )) +; MQTPGLLDN +; LVZPSTTCZ |