aoc

Advent of Code Solutions
git clone git://git.alexkarle.com.com/aoc
Log | Files | Refs | README | LICENSE

commit c4a72dd0f20dafb5fc857de6746cad406ab9acd4 (patch)
parent ecb091548b832afd3722b2c83b949396754feccc
Author: Alex Karle <alex@alexkarle.com>
Date:   Mon,  5 Dec 2022 22:26:56 -0500

day5: Add part 1

Diffstat:
A2022/05/input | 513+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2022/05/sample | 9+++++++++
A2022/05/sol.scm | 108+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 630 insertions(+), 0 deletions(-)

diff --git a/2022/05/input b/2022/05/input @@ -0,0 +1,513 @@ +[D] [N] [F] +[H] [F] [L] [J] [H] +[R] [H] [F] [V] [G] [H] +[Z] [Q] [Z] [W] [L] [J] [B] +[S] [W] [H] [B] [H] [D] [C] [M] +[P] [R] [S] [G] [J] [J] [W] [Z] [V] +[W] [B] [V] [F] [G] [T] [T] [T] [P] +[Q] [V] [C] [H] [P] [Q] [Z] [D] [W] + 1 2 3 4 5 6 7 8 9 + +move 1 from 3 to 9 +move 2 from 2 to 1 +move 3 from 5 to 4 +move 1 from 1 to 8 +move 1 from 3 to 9 +move 1 from 5 to 7 +move 1 from 5 to 3 +move 4 from 4 to 2 +move 2 from 3 to 4 +move 1 from 3 to 2 +move 6 from 1 to 5 +move 1 from 4 to 3 +move 1 from 3 to 9 +move 4 from 2 to 4 +move 4 from 8 to 7 +move 3 from 2 to 6 +move 1 from 2 to 7 +move 5 from 5 to 6 +move 1 from 5 to 8 +move 5 from 8 to 7 +move 7 from 4 to 6 +move 15 from 6 to 4 +move 1 from 8 to 7 +move 1 from 1 to 5 +move 1 from 2 to 4 +move 2 from 4 to 8 +move 1 from 5 to 2 +move 5 from 6 to 4 +move 2 from 2 to 1 +move 1 from 9 to 4 +move 1 from 6 to 9 +move 3 from 9 to 3 +move 3 from 4 to 3 +move 1 from 6 to 1 +move 5 from 3 to 4 +move 2 from 8 to 5 +move 1 from 3 to 6 +move 1 from 6 to 2 +move 1 from 2 to 8 +move 6 from 4 to 2 +move 1 from 2 to 7 +move 1 from 5 to 3 +move 4 from 9 to 3 +move 1 from 9 to 1 +move 3 from 1 to 6 +move 1 from 9 to 7 +move 14 from 7 to 6 +move 1 from 8 to 3 +move 4 from 2 to 6 +move 3 from 3 to 8 +move 9 from 4 to 9 +move 1 from 1 to 5 +move 2 from 5 to 8 +move 3 from 8 to 2 +move 4 from 2 to 6 +move 1 from 3 to 9 +move 10 from 6 to 1 +move 5 from 9 to 8 +move 1 from 9 to 3 +move 6 from 1 to 8 +move 3 from 7 to 4 +move 2 from 4 to 5 +move 2 from 9 to 8 +move 15 from 8 to 3 +move 3 from 7 to 9 +move 8 from 4 to 3 +move 2 from 5 to 9 +move 6 from 6 to 5 +move 6 from 5 to 8 +move 1 from 7 to 8 +move 6 from 9 to 2 +move 5 from 2 to 4 +move 6 from 3 to 5 +move 5 from 5 to 8 +move 1 from 5 to 7 +move 1 from 9 to 7 +move 2 from 6 to 4 +move 12 from 8 to 2 +move 7 from 2 to 4 +move 3 from 7 to 5 +move 3 from 5 to 7 +move 3 from 7 to 9 +move 2 from 9 to 7 +move 1 from 9 to 3 +move 2 from 7 to 4 +move 3 from 1 to 9 +move 4 from 6 to 5 +move 6 from 2 to 8 +move 14 from 4 to 9 +move 7 from 9 to 6 +move 9 from 9 to 2 +move 1 from 5 to 8 +move 5 from 6 to 3 +move 3 from 1 to 9 +move 3 from 8 to 9 +move 1 from 8 to 3 +move 5 from 2 to 5 +move 1 from 4 to 9 +move 2 from 6 to 1 +move 2 from 3 to 6 +move 3 from 8 to 3 +move 2 from 6 to 3 +move 1 from 4 to 9 +move 4 from 3 to 6 +move 7 from 6 to 9 +move 10 from 9 to 2 +move 10 from 3 to 2 +move 7 from 2 to 8 +move 2 from 1 to 7 +move 13 from 3 to 7 +move 7 from 5 to 1 +move 1 from 9 to 6 +move 4 from 8 to 4 +move 2 from 3 to 2 +move 4 from 4 to 6 +move 1 from 3 to 4 +move 5 from 6 to 5 +move 3 from 5 to 7 +move 12 from 2 to 5 +move 7 from 5 to 6 +move 2 from 8 to 3 +move 7 from 6 to 2 +move 3 from 9 to 6 +move 1 from 6 to 7 +move 1 from 4 to 9 +move 2 from 7 to 6 +move 13 from 7 to 4 +move 3 from 7 to 5 +move 1 from 9 to 6 +move 12 from 4 to 3 +move 1 from 8 to 1 +move 2 from 6 to 4 +move 1 from 7 to 9 +move 2 from 9 to 8 +move 12 from 3 to 5 +move 1 from 8 to 2 +move 15 from 5 to 6 +move 2 from 4 to 6 +move 1 from 9 to 6 +move 5 from 5 to 4 +move 4 from 4 to 2 +move 2 from 1 to 5 +move 4 from 1 to 5 +move 1 from 8 to 6 +move 7 from 5 to 2 +move 22 from 2 to 3 +move 9 from 6 to 3 +move 1 from 1 to 8 +move 1 from 8 to 7 +move 23 from 3 to 6 +move 2 from 2 to 4 +move 1 from 7 to 8 +move 1 from 8 to 2 +move 19 from 6 to 9 +move 2 from 2 to 4 +move 4 from 4 to 6 +move 13 from 6 to 8 +move 12 from 9 to 1 +move 2 from 5 to 9 +move 2 from 4 to 8 +move 1 from 2 to 7 +move 1 from 7 to 1 +move 4 from 6 to 2 +move 10 from 1 to 9 +move 1 from 6 to 7 +move 11 from 8 to 2 +move 6 from 3 to 6 +move 1 from 7 to 2 +move 1 from 1 to 8 +move 2 from 6 to 7 +move 7 from 6 to 3 +move 9 from 3 to 1 +move 7 from 9 to 6 +move 1 from 8 to 7 +move 4 from 2 to 6 +move 1 from 8 to 3 +move 6 from 6 to 5 +move 9 from 9 to 3 +move 5 from 6 to 1 +move 1 from 7 to 8 +move 2 from 8 to 4 +move 1 from 4 to 2 +move 1 from 4 to 5 +move 2 from 5 to 6 +move 1 from 6 to 9 +move 9 from 1 to 4 +move 4 from 4 to 6 +move 2 from 4 to 7 +move 7 from 2 to 8 +move 5 from 6 to 7 +move 6 from 3 to 8 +move 8 from 1 to 9 +move 3 from 5 to 2 +move 2 from 3 to 9 +move 3 from 9 to 4 +move 7 from 2 to 3 +move 1 from 7 to 2 +move 10 from 3 to 2 +move 6 from 9 to 4 +move 1 from 3 to 1 +move 1 from 1 to 8 +move 4 from 8 to 5 +move 10 from 8 to 4 +move 2 from 8 to 9 +move 7 from 4 to 9 +move 6 from 2 to 6 +move 3 from 6 to 5 +move 4 from 4 to 9 +move 8 from 7 to 5 +move 1 from 9 to 2 +move 7 from 2 to 1 +move 4 from 9 to 8 +move 2 from 6 to 3 +move 2 from 3 to 2 +move 13 from 5 to 7 +move 5 from 4 to 9 +move 5 from 1 to 7 +move 3 from 5 to 8 +move 17 from 7 to 2 +move 15 from 2 to 6 +move 15 from 9 to 5 +move 1 from 9 to 5 +move 4 from 8 to 6 +move 1 from 4 to 6 +move 5 from 4 to 7 +move 5 from 2 to 7 +move 18 from 6 to 2 +move 2 from 7 to 6 +move 10 from 2 to 8 +move 2 from 2 to 3 +move 11 from 8 to 7 +move 7 from 7 to 5 +move 9 from 7 to 5 +move 3 from 7 to 5 +move 2 from 1 to 7 +move 4 from 2 to 1 +move 30 from 5 to 1 +move 1 from 3 to 1 +move 35 from 1 to 9 +move 2 from 2 to 5 +move 2 from 8 to 3 +move 20 from 9 to 2 +move 3 from 7 to 9 +move 1 from 3 to 6 +move 5 from 5 to 3 +move 18 from 2 to 5 +move 4 from 5 to 8 +move 7 from 9 to 7 +move 1 from 6 to 2 +move 3 from 8 to 5 +move 6 from 3 to 5 +move 3 from 7 to 4 +move 2 from 2 to 3 +move 1 from 4 to 5 +move 2 from 4 to 5 +move 4 from 7 to 2 +move 26 from 5 to 6 +move 2 from 2 to 7 +move 1 from 2 to 9 +move 1 from 7 to 8 +move 1 from 5 to 3 +move 2 from 8 to 3 +move 11 from 9 to 3 +move 6 from 3 to 4 +move 27 from 6 to 4 +move 33 from 4 to 3 +move 4 from 6 to 8 +move 1 from 2 to 8 +move 1 from 7 to 3 +move 4 from 8 to 9 +move 1 from 8 to 6 +move 34 from 3 to 8 +move 1 from 8 to 5 +move 1 from 2 to 9 +move 8 from 3 to 9 +move 3 from 5 to 4 +move 1 from 6 to 5 +move 27 from 8 to 9 +move 1 from 3 to 4 +move 1 from 5 to 7 +move 3 from 8 to 1 +move 11 from 9 to 1 +move 1 from 7 to 5 +move 11 from 9 to 3 +move 1 from 5 to 1 +move 1 from 8 to 7 +move 2 from 9 to 2 +move 1 from 2 to 1 +move 1 from 2 to 7 +move 2 from 8 to 2 +move 6 from 3 to 8 +move 1 from 4 to 2 +move 7 from 1 to 2 +move 1 from 7 to 1 +move 19 from 9 to 1 +move 3 from 2 to 9 +move 10 from 1 to 4 +move 2 from 9 to 1 +move 1 from 7 to 9 +move 7 from 1 to 6 +move 10 from 4 to 3 +move 14 from 1 to 7 +move 2 from 9 to 1 +move 3 from 4 to 6 +move 9 from 7 to 6 +move 1 from 3 to 5 +move 4 from 8 to 5 +move 10 from 6 to 8 +move 3 from 5 to 6 +move 10 from 3 to 4 +move 4 from 3 to 7 +move 1 from 5 to 9 +move 2 from 7 to 9 +move 1 from 1 to 9 +move 6 from 2 to 4 +move 1 from 5 to 3 +move 11 from 4 to 9 +move 3 from 4 to 9 +move 1 from 2 to 7 +move 2 from 3 to 5 +move 1 from 3 to 2 +move 7 from 7 to 2 +move 2 from 5 to 8 +move 8 from 2 to 1 +move 2 from 6 to 8 +move 9 from 6 to 8 +move 3 from 8 to 2 +move 3 from 2 to 6 +move 9 from 9 to 5 +move 3 from 5 to 8 +move 5 from 9 to 4 +move 3 from 6 to 4 +move 1 from 6 to 3 +move 3 from 1 to 6 +move 3 from 6 to 9 +move 17 from 8 to 5 +move 12 from 5 to 4 +move 21 from 4 to 3 +move 1 from 4 to 9 +move 7 from 5 to 4 +move 22 from 3 to 7 +move 3 from 1 to 8 +move 3 from 9 to 1 +move 4 from 4 to 6 +move 1 from 6 to 2 +move 3 from 4 to 1 +move 1 from 6 to 7 +move 4 from 9 to 3 +move 2 from 5 to 7 +move 1 from 9 to 6 +move 2 from 6 to 9 +move 8 from 7 to 9 +move 1 from 6 to 2 +move 1 from 9 to 3 +move 4 from 3 to 4 +move 14 from 7 to 4 +move 1 from 3 to 2 +move 3 from 7 to 8 +move 12 from 8 to 9 +move 8 from 4 to 1 +move 1 from 7 to 4 +move 2 from 5 to 1 +move 3 from 2 to 9 +move 17 from 9 to 3 +move 6 from 9 to 1 +move 1 from 9 to 2 +move 13 from 3 to 9 +move 4 from 3 to 1 +move 3 from 9 to 1 +move 22 from 1 to 9 +move 1 from 8 to 1 +move 6 from 9 to 5 +move 4 from 1 to 9 +move 3 from 1 to 9 +move 4 from 4 to 8 +move 4 from 4 to 2 +move 1 from 4 to 3 +move 3 from 8 to 9 +move 1 from 3 to 4 +move 1 from 1 to 3 +move 1 from 8 to 2 +move 1 from 5 to 8 +move 4 from 2 to 1 +move 1 from 8 to 7 +move 10 from 9 to 6 +move 1 from 7 to 9 +move 1 from 2 to 3 +move 1 from 6 to 1 +move 3 from 5 to 7 +move 1 from 8 to 7 +move 1 from 6 to 1 +move 1 from 2 to 4 +move 1 from 5 to 2 +move 19 from 9 to 2 +move 1 from 4 to 7 +move 1 from 3 to 7 +move 3 from 7 to 9 +move 4 from 1 to 2 +move 10 from 9 to 4 +move 1 from 5 to 8 +move 3 from 6 to 4 +move 1 from 3 to 4 +move 10 from 2 to 8 +move 12 from 2 to 5 +move 3 from 5 to 9 +move 5 from 6 to 5 +move 5 from 1 to 4 +move 22 from 4 to 3 +move 3 from 8 to 7 +move 1 from 7 to 2 +move 3 from 2 to 9 +move 19 from 3 to 5 +move 2 from 7 to 8 +move 7 from 5 to 6 +move 5 from 9 to 6 +move 1 from 9 to 3 +move 16 from 5 to 1 +move 2 from 3 to 1 +move 3 from 7 to 3 +move 7 from 8 to 4 +move 2 from 8 to 1 +move 5 from 5 to 9 +move 1 from 5 to 2 +move 1 from 2 to 3 +move 1 from 8 to 5 +move 4 from 5 to 7 +move 2 from 3 to 8 +move 2 from 1 to 5 +move 4 from 7 to 6 +move 6 from 4 to 7 +move 4 from 9 to 8 +move 14 from 6 to 7 +move 8 from 1 to 7 +move 7 from 1 to 3 +move 3 from 5 to 9 +move 28 from 7 to 5 +move 1 from 1 to 8 +move 4 from 8 to 3 +move 9 from 3 to 1 +move 1 from 9 to 5 +move 6 from 3 to 2 +move 10 from 1 to 6 +move 1 from 1 to 9 +move 5 from 9 to 7 +move 14 from 5 to 3 +move 1 from 4 to 1 +move 1 from 7 to 2 +move 1 from 7 to 1 +move 1 from 1 to 7 +move 3 from 8 to 5 +move 4 from 6 to 3 +move 3 from 7 to 2 +move 15 from 3 to 6 +move 16 from 5 to 7 +move 4 from 2 to 8 +move 1 from 3 to 1 +move 5 from 7 to 3 +move 12 from 6 to 4 +move 4 from 8 to 5 +move 1 from 4 to 2 +move 2 from 5 to 3 +move 8 from 6 to 3 +move 7 from 4 to 5 +move 9 from 7 to 6 +move 1 from 7 to 9 +move 1 from 1 to 9 +move 1 from 1 to 9 +move 5 from 2 to 8 +move 5 from 8 to 2 +move 11 from 5 to 9 +move 1 from 4 to 2 +move 4 from 9 to 6 +move 12 from 3 to 7 +move 3 from 4 to 9 +move 14 from 6 to 2 +move 2 from 2 to 4 +move 2 from 3 to 5 +move 10 from 7 to 2 +move 1 from 4 to 8 +move 1 from 2 to 7 +move 28 from 2 to 9 +move 4 from 7 to 5 +move 1 from 2 to 4 +move 6 from 5 to 1 +move 2 from 4 to 3 +move 1 from 8 to 1 +move 40 from 9 to 1 +move 10 from 1 to 6 +move 5 from 3 to 5 +move 1 from 9 to 8 +move 3 from 6 to 7 +move 11 from 1 to 2 +move 9 from 2 to 3 +move 3 from 5 to 1 +move 4 from 7 to 1 +move 2 from 2 to 4 +move 2 from 5 to 8 +move 19 from 1 to 7 +move 8 from 3 to 2 +move 14 from 1 to 8 +move 14 from 7 to 1 +move 4 from 6 to 5 +move 1 from 1 to 9 diff --git a/2022/05/sample b/2022/05/sample @@ -0,0 +1,9 @@ + [D] +[N] [C] +[Z] [M] [P] + 1 2 3 + +move 1 from 2 to 1 +move 3 from 1 to 3 +move 2 from 2 to 1 +move 1 from 1 to 2 diff --git a/2022/05/sol.scm b/2022/05/sol.scm @@ -0,0 +1,108 @@ +#!/usr/local/bin/chicken-csi -ss +(import (chicken io) + (chicken string)) + +; utils +(define (range n) + (if (equal? n 0) + '(0) + (append (range (sub1 n)) (list n)))) + +(define (filter pred lst) + (if (null? lst) + '() + (if (pred (car lst)) + (cons (car lst) (filter pred (cdr lst))) + (filter pred (cdr lst))))) + +; [D] +; [N] [C] => ((N Z) (D C M) (P)) +; [Z] [M] [P] +; 1 2 3 + +; key insight: chars are at 1 5 ... N - 2 (where n = length) +(define (get-mask n) + (filter (lambda (x) (eq? (modulo x 4) 1)) + (range n))) + +(define (get-boxes line) + (map (lambda (k) (substring line k (add1 k))) + (get-mask (string-length line)))) + +(define (add-layer boxes stacks) + (cond ((null? boxes) + '()) + ((equal? (car boxes) " ") + (cons (car stacks) (add-layer (cdr boxes) (cdr stacks)))) + (else + (cons (cons (car boxes) (car stacks)) + (add-layer (cdr boxes) (cdr stacks)))))) + +(define (init-stacks n) + (map (lambda (x) '()) (get-mask n))) + +(define (parse-boxes lines) + (let ((n (string-length (car lines)))) + (let loop ((lines lines) (stacks (init-stacks n))) + (let ((l (car lines))) + (if (equal? l "") + (map (lambda (s) (reverse (cdr s))) stacks) + (loop (cdr lines) (add-layer (get-boxes l) stacks))))))) + +; move N from A to B -> (N A B) +(define (parse-move x) + (let* ((parts (string-split x)) + (count (string->number (list-ref parts 1))) + (from (sub1 (string->number (list-ref parts 3)))) + (to (sub1 (string->number (list-ref parts 5))))) + (list count from to))) + +; TODO: this could be filter + map +(define (parse-moves lines) + (cond ((null? lines) '()) + ((and (> (string-length (car lines)) 0) + (equal? (substring (car lines) 0 1) "m")) + (cons (parse-move (car lines)) (parse-moves (cdr lines)))) + (else (parse-moves (cdr lines))))) + +(define (pop-box stacks k) + (if (>= k (length stacks)) + (error "Bad pop") + (if (equal? k 0) + (values (caar stacks) (cons (cdar stacks) (cdr stacks))) + (let-values (((b sts) (pop-box (cdr stacks) (sub1 k)))) + (values b (cons (car stacks) sts)))))) + +(define (push-box stacks k e) + (if (>= k (length stacks)) + (error "Bad push") + (if (equal? k 0) + (cons (cons e (car stacks)) (cdr stacks)) + (cons (car stacks) (push-box (cdr stacks) (sub1 k) e))))) + +(define (play-move move stacks) + (let ((n (car move)) + (f (cadr move)) + (t (caddr move))) + (if (equal? n 0) + stacks + (let-values (((box sts) (pop-box stacks f))) + (play-move (cons (sub1 n) (cdr move)) + (push-box sts t box)))))) + +(define (play-moves moves stacks) + (if (null? moves) + stacks + (play-moves (cdr moves) (play-move (car moves) stacks)))) + +; TODO: add1/sub1 on stack nums, iterate, profit +(define (main args) + (let* ((lines (read-lines)) + (stacks (parse-boxes lines)) + (moves (parse-moves lines))) + (for-each (lambda (s) (display (car s))) + (play-moves moves stacks)) + (newline))) + +; test data for the REPL +(define sks '((N Z) (D C M) (P)))