38 lines
1.4 KiB
Racket
38 lines
1.4 KiB
Racket
#lang curly-fn racket
|
|
|
|
(require "scripts/aoc.rkt")
|
|
|
|
;; solution for day 5
|
|
|
|
(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 (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"))
|
|
(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)])
|
|
(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"))])
|
|
(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"))
|
|
(answer 5 1 (time (part1 input)))
|
|
(answer 5 2 (time (part2 input)))
|
|
(displayln "meow"))
|