From 8bc5a269e0081332e4584857fb602e7d907443e9 Mon Sep 17 00:00:00 2001 From: xenia Date: Mon, 5 Dec 2022 00:48:12 -0500 Subject: [PATCH] day 5: idk maybe slightly better --- 5.rkt | 61 +++++++++++++++++++---------------------------------------- 1 file changed, 19 insertions(+), 42 deletions(-) diff --git a/5.rkt b/5.rkt index 33268b3..b2d5d01 100644 --- a/5.rkt +++ b/5.rkt @@ -4,54 +4,31 @@ ;; solution for day 5 -(define (part1 input) - (match-define (list stacks moves) input) - (define end-stacks - (for/fold ([stacks stacks]) ([move (in-list moves)]) - (match-define (list ct (app sub1 from) (app sub1 to)) move) - (define movement (take (list-ref stacks from) ct)) - (for/list ([stack (in-list stacks)] [i (in-naturals)]) - (match i - [(== from) (drop stack ct)] - [(== to) (append (reverse movement) stack)] - [else stack])))) - (apply string-append (map first end-stacks))) +(define (solution input reverse?) + (for/fold ([stacks (first input)] #:result (list->string (map first stacks))) + ([move (in-list (second input))]) + (match-define (list ct (app sub1 from) (app sub1 to)) move) + (define movement ((if reverse? reverse identity) + (take (list-ref stacks from) ct))) + (list-update (list-update stacks from #{drop % ct}) + to #{append movement %}))) -(define (part2 input) - (match-define (list stacks moves) input) - (define end-stacks - (for/fold ([stacks stacks]) ([move (in-list moves)]) - (match-define (list ct (app sub1 from) (app sub1 to)) move) - (define movement (take (list-ref stacks from) ct)) - (for/list ([stack (in-list stacks)] [i (in-naturals)]) - (match i - [(== from) (drop stack ct)] - [(== to) (append movement stack)] - [else stack])))) - (apply string-append (map first end-stacks))) +(define (part1 input) (solution input #t)) +(define (part2 input) (solution input #f)) (define (parse fname) (match-define (list stacks moves) (string-split (file->string fname) "\n\n")) - (define stack-lines (string-split stacks "\n")) - (define columns - (for/list ([x (in-list (string->list (last stack-lines)))] - [i (in-naturals)] - #:unless (equal? x #\space)) - i)) - (define stacks-data + (match-define (list stack-lines ... (app string->list cols-line)) (string-split stacks "\n")) + (define columns (indexes-where cols-line #{not (equal? % #\space)})) + (list (for/list ([col (in-list columns)]) - (reverse - (for*/list ([line (in-list (rest (reverse stack-lines)))] - [letter (in-value (substring line col (add1 col)))] - #:unless (equal? " " letter)) - letter)))) - (define moves-data + (for*/list ([line (in-list stack-lines)] + [letter (in-value (string-ref line col))] + #:unless (equal? #\space letter)) + letter)) (for/list ([line (in-list (string-split moves "\n"))]) - (define parts (string-split line " ")) - (list (string->number (second parts)) - (string->number (fourth parts)) - (string->number (sixth parts))))) - (list stacks-data moves-data)) + (match-define (pregexp #px"move (\\d+) from (\\d+) to (\\d+)" (app rest move-data)) line) + (map string->number move-data)))) (module+ main (define input (parse "inputs/5"))