day 5: idk maybe slightly better
This commit is contained in:
parent
ff0cf8d8dd
commit
8bc5a269e0
59
5.rkt
59
5.rkt
|
@ -4,54 +4,31 @@
|
||||||
|
|
||||||
;; solution for day 5
|
;; solution for day 5
|
||||||
|
|
||||||
(define (part1 input)
|
(define (solution input reverse?)
|
||||||
(match-define (list stacks moves) input)
|
(for/fold ([stacks (first input)] #:result (list->string (map first stacks)))
|
||||||
(define end-stacks
|
([move (in-list (second input))])
|
||||||
(for/fold ([stacks stacks]) ([move (in-list moves)])
|
|
||||||
(match-define (list ct (app sub1 from) (app sub1 to)) move)
|
(match-define (list ct (app sub1 from) (app sub1 to)) move)
|
||||||
(define movement (take (list-ref stacks from) ct))
|
(define movement ((if reverse? reverse identity)
|
||||||
(for/list ([stack (in-list stacks)] [i (in-naturals)])
|
(take (list-ref stacks from) ct)))
|
||||||
(match i
|
(list-update (list-update stacks from #{drop % ct})
|
||||||
[(== from) (drop stack ct)]
|
to #{append movement %})))
|
||||||
[(== to) (append (reverse movement) stack)]
|
|
||||||
[else stack]))))
|
|
||||||
(apply string-append (map first end-stacks)))
|
|
||||||
|
|
||||||
(define (part2 input)
|
(define (part1 input) (solution input #t))
|
||||||
(match-define (list stacks moves) input)
|
(define (part2 input) (solution input #f))
|
||||||
(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 (parse fname)
|
(define (parse fname)
|
||||||
(match-define (list stacks moves) (string-split (file->string fname) "\n\n"))
|
(match-define (list stacks moves) (string-split (file->string fname) "\n\n"))
|
||||||
(define stack-lines (string-split stacks "\n"))
|
(match-define (list stack-lines ... (app string->list cols-line)) (string-split stacks "\n"))
|
||||||
(define columns
|
(define columns (indexes-where cols-line #{not (equal? % #\space)}))
|
||||||
(for/list ([x (in-list (string->list (last stack-lines)))]
|
(list
|
||||||
[i (in-naturals)]
|
|
||||||
#:unless (equal? x #\space))
|
|
||||||
i))
|
|
||||||
(define stacks-data
|
|
||||||
(for/list ([col (in-list columns)])
|
(for/list ([col (in-list columns)])
|
||||||
(reverse
|
(for*/list ([line (in-list stack-lines)]
|
||||||
(for*/list ([line (in-list (rest (reverse stack-lines)))]
|
[letter (in-value (string-ref line col))]
|
||||||
[letter (in-value (substring line col (add1 col)))]
|
#:unless (equal? #\space letter))
|
||||||
#:unless (equal? " " letter))
|
letter))
|
||||||
letter))))
|
|
||||||
(define moves-data
|
|
||||||
(for/list ([line (in-list (string-split moves "\n"))])
|
(for/list ([line (in-list (string-split moves "\n"))])
|
||||||
(define parts (string-split line " "))
|
(match-define (pregexp #px"move (\\d+) from (\\d+) to (\\d+)" (app rest move-data)) line)
|
||||||
(list (string->number (second parts))
|
(map string->number move-data))))
|
||||||
(string->number (fourth parts))
|
|
||||||
(string->number (sixth parts)))))
|
|
||||||
(list stacks-data moves-data))
|
|
||||||
|
|
||||||
(module+ main
|
(module+ main
|
||||||
(define input (parse "inputs/5"))
|
(define input (parse "inputs/5"))
|
||||||
|
|
Loading…
Reference in New Issue