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
|
||||
|
||||
(define (part1 input)
|
||||
(match-define (list stacks moves) input)
|
||||
(define end-stacks
|
||||
(for/fold ([stacks stacks]) ([move (in-list moves)])
|
||||
(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 (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 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"))
|
||||
|
|
Loading…
Reference in New Issue