This commit is contained in:
xenia 2022-12-05 00:25:04 -05:00
parent 60425f6658
commit ff0cf8d8dd
2 changed files with 571 additions and 0 deletions

60
5.rkt Normal file
View File

@ -0,0 +1,60 @@
#lang curly-fn racket
(require "scripts/aoc.rkt")
;; 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 (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 (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
(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 (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))
(module+ main
(define input (parse "inputs/5"))
(answer 5 1 (time (part1 input)))
(answer 5 2 (time (part2 input)))
(displayln "meow"))

511
inputs/5 Normal file
View File

@ -0,0 +1,511 @@
[J] [B] [W]
[T] [W] [F] [R] [Z]
[Q] [M] [J] [R] [W] [H]
[F] [L] [P] [R] [N] [Z] [G]
[F] [M] [S] [Q] [M] [P] [S] [C]
[L] [V] [R] [V] [W] [P] [C] [P] [J]
[M] [Z] [V] [S] [S] [V] [Q] [H] [M]
[W] [B] [H] [F] [L] [F] [J] [V] [B]
1 2 3 4 5 6 7 8 9
move 3 from 5 to 7
move 2 from 8 to 9
move 4 from 3 to 5
move 2 from 1 to 7
move 1 from 3 to 6
move 2 from 1 to 7
move 1 from 8 to 7
move 4 from 2 to 8
move 10 from 9 to 1
move 6 from 6 to 2
move 1 from 6 to 7
move 9 from 8 to 6
move 4 from 2 to 4
move 2 from 4 to 1
move 6 from 1 to 6
move 1 from 3 to 2
move 2 from 1 to 4
move 2 from 4 to 3
move 2 from 1 to 3
move 4 from 3 to 1
move 15 from 7 to 9
move 4 from 5 to 9
move 13 from 9 to 4
move 10 from 4 to 8
move 1 from 7 to 4
move 6 from 9 to 5
move 11 from 6 to 7
move 4 from 5 to 7
move 3 from 8 to 7
move 4 from 2 to 4
move 1 from 5 to 1
move 5 from 8 to 4
move 1 from 5 to 4
move 10 from 7 to 1
move 8 from 7 to 9
move 12 from 1 to 9
move 8 from 9 to 1
move 2 from 6 to 9
move 2 from 8 to 4
move 1 from 6 to 9
move 13 from 4 to 2
move 13 from 4 to 2
move 1 from 6 to 1
move 1 from 6 to 4
move 1 from 4 to 5
move 14 from 1 to 8
move 1 from 5 to 4
move 13 from 9 to 5
move 9 from 8 to 2
move 8 from 2 to 1
move 5 from 8 to 2
move 5 from 1 to 6
move 3 from 1 to 3
move 1 from 4 to 8
move 9 from 5 to 9
move 18 from 2 to 8
move 3 from 3 to 5
move 2 from 6 to 4
move 14 from 2 to 7
move 1 from 4 to 2
move 1 from 6 to 9
move 1 from 2 to 5
move 1 from 6 to 2
move 1 from 4 to 6
move 6 from 8 to 1
move 2 from 6 to 9
move 5 from 5 to 3
move 1 from 7 to 8
move 10 from 9 to 7
move 13 from 8 to 5
move 5 from 5 to 2
move 6 from 5 to 7
move 1 from 8 to 5
move 5 from 5 to 9
move 5 from 9 to 7
move 4 from 3 to 8
move 6 from 1 to 6
move 4 from 2 to 4
move 3 from 7 to 5
move 2 from 2 to 9
move 1 from 3 to 7
move 29 from 7 to 9
move 4 from 5 to 2
move 5 from 6 to 4
move 3 from 7 to 9
move 3 from 8 to 6
move 1 from 2 to 6
move 3 from 2 to 5
move 1 from 8 to 4
move 1 from 5 to 9
move 8 from 4 to 9
move 15 from 9 to 2
move 1 from 5 to 1
move 10 from 9 to 4
move 5 from 4 to 5
move 5 from 5 to 4
move 1 from 1 to 9
move 1 from 4 to 3
move 8 from 2 to 4
move 7 from 2 to 7
move 1 from 3 to 8
move 1 from 5 to 6
move 4 from 7 to 3
move 1 from 8 to 2
move 7 from 4 to 7
move 11 from 9 to 7
move 5 from 4 to 2
move 3 from 9 to 6
move 3 from 3 to 8
move 4 from 2 to 4
move 5 from 9 to 5
move 1 from 2 to 1
move 3 from 8 to 5
move 2 from 9 to 1
move 1 from 2 to 5
move 2 from 9 to 6
move 3 from 7 to 5
move 7 from 4 to 1
move 4 from 4 to 9
move 3 from 7 to 2
move 3 from 1 to 9
move 1 from 2 to 3
move 2 from 7 to 9
move 6 from 5 to 4
move 6 from 4 to 3
move 5 from 5 to 1
move 6 from 7 to 8
move 1 from 5 to 1
move 2 from 9 to 4
move 1 from 4 to 3
move 10 from 6 to 4
move 2 from 2 to 1
move 6 from 4 to 1
move 5 from 8 to 3
move 1 from 8 to 2
move 7 from 3 to 9
move 1 from 6 to 9
move 2 from 7 to 3
move 20 from 1 to 6
move 7 from 3 to 8
move 2 from 9 to 6
move 1 from 2 to 3
move 2 from 3 to 6
move 1 from 1 to 4
move 6 from 4 to 7
move 5 from 8 to 3
move 22 from 6 to 4
move 2 from 9 to 7
move 3 from 3 to 4
move 6 from 4 to 2
move 11 from 9 to 3
move 9 from 3 to 7
move 5 from 4 to 2
move 5 from 7 to 2
move 5 from 7 to 6
move 10 from 2 to 4
move 3 from 2 to 1
move 1 from 6 to 3
move 1 from 1 to 7
move 17 from 4 to 1
move 1 from 8 to 4
move 2 from 7 to 5
move 3 from 2 to 5
move 3 from 3 to 8
move 4 from 5 to 1
move 3 from 3 to 7
move 1 from 4 to 5
move 21 from 1 to 5
move 3 from 8 to 3
move 4 from 7 to 5
move 1 from 1 to 7
move 1 from 6 to 3
move 4 from 4 to 1
move 1 from 8 to 1
move 3 from 4 to 9
move 5 from 1 to 8
move 3 from 9 to 3
move 5 from 6 to 1
move 5 from 1 to 4
move 6 from 3 to 2
move 1 from 3 to 2
move 3 from 8 to 1
move 7 from 2 to 1
move 10 from 5 to 2
move 12 from 5 to 7
move 2 from 8 to 3
move 5 from 5 to 8
move 8 from 1 to 6
move 5 from 4 to 5
move 3 from 8 to 6
move 1 from 8 to 3
move 6 from 6 to 7
move 2 from 3 to 8
move 3 from 2 to 1
move 6 from 2 to 9
move 2 from 8 to 4
move 1 from 3 to 9
move 1 from 8 to 6
move 1 from 6 to 9
move 7 from 9 to 5
move 1 from 9 to 7
move 1 from 4 to 6
move 2 from 6 to 5
move 1 from 4 to 1
move 1 from 2 to 7
move 5 from 1 to 2
move 10 from 7 to 4
move 12 from 5 to 7
move 6 from 4 to 8
move 2 from 5 to 6
move 1 from 8 to 9
move 1 from 9 to 5
move 30 from 7 to 9
move 4 from 8 to 4
move 1 from 8 to 7
move 2 from 1 to 4
move 6 from 6 to 3
move 1 from 4 to 1
move 1 from 1 to 2
move 8 from 4 to 8
move 1 from 4 to 5
move 2 from 5 to 6
move 2 from 9 to 8
move 3 from 2 to 1
move 4 from 3 to 2
move 1 from 6 to 4
move 1 from 7 to 1
move 2 from 8 to 2
move 1 from 9 to 2
move 2 from 3 to 2
move 1 from 4 to 2
move 4 from 9 to 6
move 3 from 6 to 4
move 21 from 9 to 8
move 13 from 2 to 7
move 9 from 8 to 5
move 3 from 1 to 4
move 14 from 7 to 2
move 5 from 8 to 9
move 1 from 1 to 2
move 7 from 8 to 6
move 2 from 8 to 2
move 8 from 6 to 9
move 1 from 4 to 5
move 5 from 8 to 2
move 4 from 5 to 9
move 9 from 9 to 6
move 2 from 7 to 6
move 1 from 8 to 7
move 9 from 6 to 4
move 1 from 6 to 5
move 1 from 7 to 3
move 1 from 4 to 7
move 1 from 7 to 2
move 9 from 2 to 3
move 8 from 4 to 1
move 8 from 9 to 2
move 2 from 6 to 5
move 4 from 5 to 2
move 2 from 9 to 5
move 1 from 4 to 9
move 10 from 3 to 7
move 1 from 9 to 2
move 1 from 5 to 3
move 7 from 2 to 8
move 7 from 1 to 5
move 1 from 1 to 2
move 2 from 8 to 2
move 1 from 3 to 5
move 2 from 8 to 6
move 2 from 8 to 9
move 2 from 4 to 6
move 3 from 2 to 8
move 3 from 6 to 7
move 7 from 5 to 8
move 7 from 2 to 7
move 1 from 6 to 8
move 5 from 2 to 7
move 6 from 8 to 3
move 2 from 7 to 1
move 7 from 2 to 5
move 1 from 3 to 5
move 1 from 1 to 5
move 2 from 9 to 7
move 4 from 3 to 7
move 2 from 4 to 6
move 1 from 1 to 6
move 1 from 2 to 4
move 16 from 5 to 6
move 1 from 4 to 9
move 19 from 6 to 1
move 1 from 3 to 5
move 1 from 9 to 1
move 1 from 8 to 5
move 5 from 8 to 3
move 5 from 7 to 2
move 3 from 2 to 9
move 5 from 1 to 7
move 2 from 5 to 1
move 3 from 9 to 4
move 4 from 1 to 9
move 2 from 2 to 8
move 2 from 8 to 6
move 1 from 6 to 9
move 4 from 3 to 8
move 4 from 8 to 3
move 2 from 3 to 8
move 1 from 8 to 2
move 1 from 9 to 7
move 10 from 1 to 7
move 26 from 7 to 6
move 3 from 9 to 3
move 1 from 4 to 6
move 2 from 1 to 4
move 1 from 1 to 6
move 1 from 9 to 3
move 1 from 2 to 3
move 4 from 4 to 9
move 10 from 7 to 8
move 3 from 7 to 4
move 4 from 9 to 4
move 4 from 4 to 7
move 4 from 3 to 9
move 5 from 7 to 5
move 3 from 5 to 1
move 3 from 9 to 8
move 3 from 1 to 5
move 2 from 3 to 5
move 7 from 8 to 1
move 7 from 8 to 9
move 4 from 6 to 3
move 3 from 3 to 6
move 1 from 3 to 4
move 2 from 4 to 1
move 1 from 9 to 6
move 4 from 1 to 3
move 3 from 5 to 1
move 1 from 5 to 2
move 6 from 1 to 2
move 6 from 2 to 7
move 2 from 7 to 4
move 1 from 2 to 6
move 1 from 1 to 4
move 3 from 5 to 7
move 6 from 7 to 4
move 1 from 9 to 3
move 1 from 3 to 6
move 4 from 4 to 3
move 9 from 6 to 1
move 10 from 1 to 6
move 7 from 4 to 5
move 28 from 6 to 4
move 3 from 6 to 7
move 3 from 3 to 8
move 4 from 5 to 7
move 1 from 8 to 4
move 18 from 4 to 7
move 8 from 7 to 6
move 6 from 4 to 1
move 2 from 5 to 4
move 8 from 6 to 1
move 2 from 8 to 9
move 1 from 5 to 3
move 1 from 9 to 1
move 5 from 9 to 2
move 2 from 9 to 3
move 1 from 2 to 5
move 2 from 1 to 5
move 6 from 7 to 5
move 1 from 6 to 4
move 6 from 5 to 9
move 2 from 4 to 1
move 8 from 1 to 8
move 4 from 9 to 7
move 1 from 5 to 6
move 1 from 1 to 6
move 2 from 1 to 2
move 1 from 9 to 7
move 3 from 2 to 4
move 2 from 8 to 3
move 5 from 8 to 2
move 4 from 2 to 5
move 1 from 8 to 9
move 12 from 3 to 2
move 2 from 6 to 2
move 12 from 2 to 4
move 6 from 2 to 3
move 4 from 1 to 9
move 8 from 4 to 7
move 3 from 3 to 4
move 1 from 5 to 4
move 5 from 9 to 6
move 3 from 5 to 8
move 1 from 9 to 1
move 2 from 8 to 5
move 3 from 5 to 6
move 1 from 8 to 4
move 4 from 7 to 8
move 1 from 1 to 3
move 2 from 8 to 3
move 7 from 6 to 7
move 1 from 3 to 7
move 2 from 8 to 6
move 22 from 7 to 8
move 6 from 4 to 8
move 5 from 8 to 6
move 5 from 6 to 2
move 4 from 2 to 3
move 6 from 8 to 5
move 4 from 4 to 7
move 1 from 3 to 7
move 4 from 4 to 5
move 1 from 5 to 4
move 2 from 6 to 5
move 9 from 5 to 6
move 10 from 6 to 7
move 1 from 2 to 1
move 3 from 4 to 8
move 16 from 7 to 9
move 1 from 7 to 8
move 1 from 1 to 8
move 1 from 8 to 3
move 2 from 7 to 4
move 15 from 8 to 1
move 1 from 8 to 1
move 4 from 8 to 4
move 7 from 9 to 7
move 3 from 5 to 9
move 10 from 9 to 6
move 2 from 9 to 2
move 7 from 7 to 4
move 9 from 3 to 2
move 8 from 2 to 7
move 1 from 8 to 4
move 3 from 2 to 1
move 9 from 7 to 1
move 9 from 4 to 1
move 2 from 7 to 5
move 1 from 5 to 4
move 1 from 5 to 2
move 6 from 1 to 3
move 16 from 1 to 2
move 9 from 2 to 1
move 5 from 6 to 9
move 2 from 1 to 9
move 1 from 2 to 5
move 4 from 4 to 8
move 2 from 8 to 2
move 2 from 2 to 3
move 17 from 1 to 2
move 2 from 1 to 9
move 13 from 2 to 8
move 1 from 2 to 4
move 11 from 8 to 3
move 3 from 3 to 4
move 3 from 9 to 2
move 1 from 5 to 2
move 1 from 9 to 3
move 3 from 4 to 3
move 1 from 4 to 9
move 3 from 3 to 4
move 1 from 8 to 7
move 7 from 2 to 9
move 3 from 1 to 7
move 3 from 2 to 8
move 3 from 7 to 9
move 10 from 3 to 5
move 3 from 6 to 9
move 8 from 9 to 4
move 1 from 2 to 1
move 1 from 7 to 9
move 2 from 2 to 3
move 4 from 4 to 8
move 1 from 6 to 2
move 7 from 5 to 3
move 1 from 5 to 2
move 9 from 8 to 9
move 12 from 3 to 8
move 1 from 1 to 9
move 9 from 8 to 6
move 1 from 5 to 7
move 1 from 5 to 4
move 2 from 2 to 9
move 1 from 2 to 6
move 2 from 4 to 3
move 9 from 4 to 8
move 6 from 3 to 6
move 12 from 6 to 2
move 2 from 6 to 7
move 8 from 8 to 3
move 5 from 8 to 7
move 3 from 6 to 5
move 6 from 3 to 7
move 6 from 7 to 6
move 1 from 4 to 9
move 4 from 6 to 5
move 20 from 9 to 6
move 4 from 9 to 8
move 2 from 8 to 7
move 4 from 6 to 4
move 10 from 6 to 1