aoc2022/5.rkt

38 lines
1.4 KiB
Racket
Raw Normal View History

2022-12-05 05:25:04 +00:00
#lang curly-fn racket
(require "scripts/aoc.rkt")
;; solution for day 5
2022-12-05 05:48:12 +00:00
(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 %})))
2022-12-05 05:25:04 +00:00
2022-12-05 05:48:12 +00:00
(define (part1 input) (solution input #t))
(define (part2 input) (solution input #f))
2022-12-05 05:25:04 +00:00
(define (parse fname)
(match-define (list stacks moves) (string-split (file->string fname) "\n\n"))
2022-12-05 05:48:12 +00:00
(match-define (list stack-lines ... (app string->list cols-line)) (string-split stacks "\n"))
(define columns (indexes-where cols-line #{not (equal? % #\space)}))
(list
2022-12-05 05:25:04 +00:00
(for/list ([col (in-list columns)])
2022-12-05 05:48:12 +00:00
(for*/list ([line (in-list stack-lines)]
[letter (in-value (string-ref line col))]
#:unless (equal? #\space letter))
letter))
2022-12-05 05:25:04 +00:00
(for/list ([line (in-list (string-split moves "\n"))])
2022-12-05 05:48:12 +00:00
(match-define (pregexp #px"move (\\d+) from (\\d+) to (\\d+)" (app rest move-data)) line)
(map string->number move-data))))
2022-12-05 05:25:04 +00:00
(module+ main
(define input (parse "inputs/5"))
(answer 5 1 (time (part1 input)))
(answer 5 2 (time (part2 input)))
(displayln "meow"))