adventofcode2022

My solutions for Advent of Code 2022
Log | Files | Refs

commit 62b95eb00088c10f536e2d8c8d3ef347bf8e3c03
parent 99d3ba50ca6735d63581a5d7cce0e75220cb1f9e
Author: Lukas Henkel <lh@entf.net>
Date:   Sat, 17 Dec 2022 11:25:44 +0100

Day 17 task 1

Diffstat:
Madventofcode2022.asd | 6++++--
Ainput/day17.txt | 1+
Asrc/day17.lisp | 116+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
At/day17.lisp | 10++++++++++
4 files changed, 131 insertions(+), 2 deletions(-)

diff --git a/adventofcode2022.asd b/adventofcode2022.asd @@ -27,7 +27,8 @@ (:file "day13") (:file "day14") (:file "day15") - (:file "day16"))) + (:file "day16") + (:file "day17"))) (defsystem "adventofcode2022/test" :description "My solutions to the advent of code 2022" @@ -53,4 +54,5 @@ (:file "day13") (:file "day14") (:file "day15") - (:file "day16"))) + (:file "day16") + (:file "day17"))) diff --git a/input/day17.txt b/input/day17.txt @@ -0,0 +1 @@ +>>><<<>>>><<<<>><<<<>><>>><>>>><><<<<>>>><<>>><<<>><>>><<>><>>><<>>><<>><>>><>>><<<<><<<>><<<>>><<>>>><>>>><<<<>>>><<<>>>><<<>>>><<<>>>><<<<><<<<><<<>>>><<<>>><<<><<<>>>><<>><<<>><<<><<<><<>><<>>>><<>>><<><<>><<><<>>><<>>>><<<><>>><<<<>>><<<<><><<<><<>>><<>><>>>><<<<><<>>><<<>>><<<<>>>><<<<>><<<>>><<<<>>><<<>>>><<<>>>><<<>>>><<<<>>><<<>>>><<<><><<<><<<><>>>><>><>>><<><<<>>><><<<>><<<<>><<>><>><>>>><<<<>>><<>>>><<><<>><<<<>><><<>>>><<<>>>><<<>><<<<>>><<<<>>>><>>>><<>><<<><<<>><<<<>>><<<<>><<>><><<<<>>><<><<<>>>><<>>>><<<<>>>><<<>>>><<<>>><<>>>><>>>><<>>><<<>>>><>>>><<<<>>>><>>>><<<><<<>>>><>><>>><>>>><<<><>><<<<><<><>>>><<><<<<><<<>>><><>>>><<><>>>><<<>>><<<<>>><>>>><<<>>><<<<>>>><<>><<<>>><<>>>><<><<<<><><<<<>>><<<<>>><<<<>>><<>>><<<>><<>>><<<<>>>><<<<>>>><<<>>><>>>><<<<>>><>><<>>><<><<<<>><<>>><<<><><<<<>>>><<<>><><<>>><<>><<><>>><<<>>>><<<<>>><<<><<>><<>>><>><<>>>><<>>>><<>>>><<>>>><<>><>>>><<><>>>><<<>>>><<<>>><<><<<<><>><<<>><<<<>><<<<>><<>>>><<<>><<>><>>>><<<>>>><<<>>>><<<<>>><<<<>><<><<<>>>><<<>>>><<>>><<><><<<<>><<<<><<<><<>><<><<<<>><<<<>><<<<><<<<>>>><<<>><<<><<<<>>><>><>>><>>><<><<<<>>><<<<>>>><<<><<<>><<<>><<<>>><<>>><<<>>><>>><<><>>>><<<<>>><<<<>>>><<<>>>><>>><>><<>><<<>><><<<>>>><<<>>><><<><<><<<<><<<<>>><<<<><>>>><<<>>>><<>>><<<<>>>><<<<>>><<>>><>>>><>><<<>>><<<<>>>><>><<>>><<<><<<>>><<<<><<<<><>>>><<><>><<<<>><<<<>>>><>>>><<>>><<<<><<<><<>>>><<><<<>>><<<><<<<>><<>>><<<<>><>>>><><<<<>>><<<>>>><><<>>>><<<<>>>><<<<>><<<>>><<<<>>><<<>>><>>>><<>><>><<<<><<<>><<>>>><>><>>>><<<>>><<><<<>><<>><<<>><<>>><<<<>><><<<>>><<<<>>><<<>>><<<<>>><<<<>>><<>>><>>><<>>><<<>><>><<<>>><<<<>><>>>><<<>>><>><>><<<>>><>>><<>>><<>><<<<>>>><<>>>><<<<>><>>><<<<>>><<<<>>>><<<>>>><>><<<>>>><<<><<>>><<><>><<<>>><<<>>><<><>>><<>>>><<<><<<<><><>>><<<<>>>><<<<><<<>><<<>><<<>>><<<>>>><>>><<<>>><<<<>><<>><<<>>>><<<><<>>><<<>><<<<>>>><<<<>><<<><<<>>><<<<><>><<<><<<<>>>><<><>><<<>>><<>>>><>>>><<<><<<<>>>><<>>>><<>><<<>>>><><<<>><<>>><<><<<<>>><<>><<<>><<<<>>>><<>>><<>>><<<<>>>><<>>><<<>><<>>>><<>><<<>>><<<>>><<>>>><<<<><>><>>><<<<>>><<<><<>><<<><<<>>><>><<<><<><<<>>><<><<>>><>><<<>>>><<><>><<<>>><<<>>>><<>><><>>><<<<>>><<>><<<><<<<>>>><>>><<<<>>><><><<<<><><<>><<>><>>>><<<<>>><<<>>>><><<<<>><<<<>><<><<<<>>><>>><<<><<<>>><<<>>><<<<>>><<>><<<<><<<<>>>><<>><<<<>>><<<>><<>>><<<><<<>>>><<>><<<<>>><<<<>>>><<<>>>><<>>><>>><<<<><<<<><><<>>>><<<>><>>>><<<>>><>><>><><<>>><<<>>>><<<>><<><<><<>><<<<><<<><>>>><>>><>><>>><<<<>>><<<><>><<<<>>><<<<><><>>>><<>>>><<>>>><<>>>><<<<>>><>><<<<>>>><<<>><<><<<<>>>><>><<>><>><>>>><<>>><>>>><<<<>><<<>>><<<>>>><<<<>><<><<<<>><>>><<>>>><<<<>><<<<>><>>>><><<<<>>>><<><<<><<>><<<<>>>><<><<<<><<<<>>>><>><<>><<><<<<>><<<><<<<>>><><<>>><>>>><<<<>><>>><<<><<>>><>>>><>><<<>>>><>>><>><<<<>>><<<><<<<>>>><<<>>><<<>><<<>>>><<<>>><<<<>>><<<<><>>><<<<>>>><>><>>><>>><<>><<<>>>><<<>><<<>>><<<>>><<>>>><<<<><<<<><<<<>>>><<<>>><<<>>>><<<><<<<>>><<<<><<<<>><<<<>><>><<<<><<<<>>><<>><><<<<><><<<<>><<>>>><<>><>><<<<><<<<>>>><<>>>><<<>>><<<<>>>><<<<>>>><<<<>><<><>>>><>>>><<><<<<><<>><<>>>><<>><<<>>><>><><<<<><<><<>><<<>>><<>>><<>>>><<<><>>>><<<<>>>><<<>><<<><<<<><<><<<><<<>>>><<<<>>>><<<<><<<<><>>><><<<<>>>><<<<><<>><<<>>>><<<<>>><>><<<<>>><<<<>>>><<<<>>><>>><<>>><<<><>>><><<<<>>><>>><>><><<<>><>>>><><<<>><<><<<>>><<><<<<>>>><<<<>>>><<<<>><<<><<<><><>>><<<><>><<>>><>>>><<<<>>><<>>><<<<>>>><<<<>>><<<>><<<<><>><<<<>>><>><<<>>><<<<>><<<<>><<>>><<<<>>>><<<>>><<>>>><<>>>><<><<<<><<>>><<<<>><<>><<<><<<<>><<<>>><<<>>>><<<<>><>>>><>><<>>>><><<>><<<<><><<<>>><<<>><<<>>>><<<<>>>><<<<>>>><<<>>>><>>>><<<>>>><>><<<>><>>><<<><>>><<><>>><<<<>><<<<>><<><<<>>>><<<<>>><<<<>>>><<<<><<<<>>><<<>>><<<>><<<>>><<<<>><<<><<<>><<<><<<<>>>><<<>>><<<><<<<>>><<<>>>><><<<<>>><><<<>>>><<<<>><>>>><<><>>>><><<<><<<<>>>><<<>><<>>><<<<><<>>><><<><<>>><<<>>><<<<><<>><<><<<>>><<>>>><<<>>><<><>>><<<>><<<>>><>>>><<<<>>>><<>>>><<>><<>><<<<><<<>>>><<<<>>>><><<<><<>><<<<>>><>><>><<>>>><<<><<<>>>><>>><<><<><<<<>>>><<<>>>><<>><><<<<>>><<>>><<<<>><>><<<<><<>>><<<>>><<<<>>><>><<<>><<>>>><<<>>><><<<>><<<<>><>>><<>><<<<>>><<><<>>><>>><><<<<>>>><<<<>><<>><<<>>><<<<>>><<>>>><<><<<<>>>><>>><<>>>><>>><<<<>>>><<<<>>><<>>><<<>>><<<><<<<><>>>><<>><><<<<>>>><>>>><<<<>><>>>><>><>>>><<<<>>>><<>>>><<><<<<>>>><<<><>>><<<<><<>>><<<><<<<><>><<<>>><<>><<<<><<<>>>><<>><<>>><<<><<<<>><><<<><>>><<<<>>>><<>>><>>>><>>><<<<>>><<<><><<>>>><<<>><<<<>>>><<><<<>>><<<<>><<<<>>><>>>><<<<>><<>>><<<<>>><>>><<<<><<<>>>><<>>><<<<><<><<<>>>><<<<>><><<<<>>>><<<>>>><<<<><<>><<>>><<<>>><>><><<>>><<>><<<<><>><<<><>>><<<<>>><<>>><>><<<>>>><<<<>><><<<>><<<>><<>><<<>><<<<>>>><<<<><<<<>>>><<<<>>><<<><<<>>><>>>><<<>>><<<<>>>><>>>><>>><<<<>>>><<>>><<>>>><<>><>>><<>>>><>>>><<<<>>>><>>><<<><<<<><>>><<<<><>>>><<<>>>><>>><<>><<><<<>><<<<><<<><<<>><<<><<>>>><<<><<>>>><<><>><<>>><<>>><>><<<>><>>>><<>>>><<>>><>>><<<<>>>><<><<>>>><<<>>>><><<>>>><<<<>><>><<><>><<><>>><>>>><<<>>>><><<<>>><<<>>>><<<>>><<<>>><>><>>><>>>><<<><<<>><>>><<><<>><<<<><<>><<>><<<>>><<>>><<<<>>><<><>>><<>>>><<<><<<>>><><<<<><<><<<>><<<<>>><><<<<><<<<><>>>><<>>>><<<<>>><<<<>><<<<>><<<><<>><<>><>><<<<>>><<<<><>><<<<><<<<><<><<>><<<><<>>><<<<>>><<<>>>><<<>><>><<<>>><<<>><>>><<>>>><<>>><<<<>><<>>>><<<<><>>><<<><<<>>><<<>>><<><>>><>>>><<<>><<<<>><><<<>>>><<<<>>><<>><<<<>>><<<>><><<>>>><<>>>><<<>>>><<<><<<>>>><<<<>><<<<>>>><<>>><<<<><>>>><>>>><>><>>>><<<>>>><>>><<<<><<<><<<<>>><><<<<>><>><<>>>><<<>><<<>>>><>><<<><>>><<<><>><<<<>><<>>>><<>><>>><<<<>>><<<>><>>><<<>>><<>>><>>><<<<>>>><<>>>><>>>><<<<>><<<>><><>><<><<<>>>><<<>>><<<>>>><<<<>><<<<><<<>>>><<>><>><<<<>>><<>>>><>>>><<<<>>><><<<>>>><<<>>><<<>>>><<<><<>><<>>>><<<<>><<<>>>><<<>><<<<>>>><<>><<>>><<>>><<>>>><<>><>>><><<>>>><>><>><>><>><<<>><<<<>><>>><<<><><<<<>>>><<<>>>><<<<>><<<<><>>><<><<<<>><<<<>><<<>>>><<<<>>><>>>><<<<>>><>><>>>><<<>>><<<<>><>>>><<><<<<>>>><<<>>>><<<><<<>>>><<<>>><<<>><>>><<<>>><<<<>>>><<<>><>>>><<>>><<>>><><<<<>>><<>><<>>><<>>><<<<>>><<<>><>>><<>>>><<<>><<<>>>><<<>><<<<>><<>>>><<<<><>>><<<><<<>>>><<>><<<<><<<>>><>>><<<<>>><<<<>><<<<>><<<<>><<<>><<<>>><<<<>>>><<<>>>><><>><>><<<>>><<<<><<<<>>><<<<><<<<><><>>><<<>>>><<<<>>>><>><<<<><<<>><>>>><<<>><>>>><<<>><>>><<>>><<<<>><><<>>><<<<><<<<><<<><<<<>>><<><<><>>>><<><<<<>><>>><<<<>>><>>><<<<><<<<>>><>>>><<>>>><<<>>>><<<>>>><>>><>><<<<><<<<>><<<<>>>><<<<>><>>><<>>><<<>>><>>><<<><><><>>><<<<><<>>>><>><<>><<<<>><<<<>>><><<<<>><<>>><<<>>>><>><><><<<><>><<<<>>><>>>><>>><<>><<<<><<>>><<<>>><<><<<>>><>>>><<<<>>>><<<><>>><>>><<<>><>>><<><<>><<><<<>><><>>><<<<>>>><<<>><<<><<<>>><<>><>>>><>>>><<<>>><<<>>><<>>>><<<<><<>>>><<>>>><<<<>>>><<<<>>>><<<>>><<>>><<>><<>>><<<<>><<<><>>>><<<>>><<<>>><<>>>><>><<>>>><<<<>>><<>>><<<<>><<>>>><<<<>>><>>>><<>>><<<>>><<<>><<<<><>>>><<><<>>><>>><<<>><<><<<<>>>><<<<>><>>>><<>>><<<>><<>>>><>><<>><<<<>><<>>><>>><<<>><<>><><<<<>>><>><<><>>>><<>>>><<<<>><><>>>><>>>><>>>><<>><<<<><>>>><<<>><<<>>><<>><<<><<<<>>><>>>><<>>><<<<>><>><<<<>><>>>><<<>><<<<>>>><<<<>>><>>>><<><<<><<>>><<><<>><<<><<><>>>><<>>><>><<<<>>>><<<<><>><<<>><>><<<><><<<<>>><<>>><<<>><<<><<<>>>><<<<>>><<<><>>>><<<><<><>><<<<>>>><<<<><<>>><<<<>>><<>>><>>>><<<>>>><<><>>><<<<>><>>><<<><<<>>>><<<<>><<<<>>><>>>><<>>><>>>><>>>><<>>>><<<>><<<><<<><<>>>><<<<><<>>><<>><>><<>><<<>>><<<<>>>><<>>><<<>><<>>><><>>>><<<>>><<<<>>>><<<<>>><<<<>><<><<<>><>><<<<><<<<>>><<<<><<<<><<<>>><<<<>>><<<<>>>><<<>>>><<<<>>><<<<><<<>>>><<><<<>>>><<<<>>>><>>>><<<<>>><<><<>><<>><<>>>><<<<><>>><<><<>><<><<<>><<>>>><<<<><>><<<><<<><<<>>>><<<<><<><<<<><<>>><>>>><<>>><<<>>>><<><<<<><<<<>><<<<><<><<>><<>>>><>><<<>>><<><<<>>>><<<>>>><<>><<><>><>><<<<>><<<>><>>>><<<<><<<>>>><<><<<<>><<<<><<<<>><<<<>>>><<<>>><<<<>>><<><<<>>>><>>>><<<>>>><<<<>><<><<<<>>><<<>>><<>><<<><><<>>>><<><<<>>>><<<>>>><<<<>>>><>>>><<<>>>><<<>>><>><<<><<<>>><<><<>>>><<<><<<>>><<>><<<>><><<<<>><><<<><<>>><<<>><<<<>>>><<>><<<<>>><<<<><>><>>>><<>><<<<>>>><<>><<<<>>>><>>>><<<>><<<>>>><><>>>><<>>><<>>><<<>><>><<><>><<<<>><<<<><<><<<<>><<<>>><<<><<<>>>><<<>>>><<<>>>><<<<>>>><<>>><<<<>><<<<>><<>>><>>><<><>><<<><><>>>><<><>>><<>>><<<>><>>><<><<<<>>><<>><<<<>>><<><>><<><<<<>>><<<>>>><>><<<<><<>>><<>><<<>>>><<<>><<<<>><<<<><<<>><>>><<<<>><<<<>>>><<<<><<>>><>>><<<<>>>><<<<>>><><><<<><<<<>>><><>><>>>><>>><>>>><<<>>>><<>><<<<><<<>>>><>>><<<<>>>><<>>>><<<<><<><<>>>><<>>><>><><>>><<<<>><>><<>>><<<>><>>><<>><<<><<>>>><<<<>>><<>>>><<<<>>><<><<<>>>><<<>>><<<>>><><<<>><<<<>>><>><<<<>><<>>><<<<><<<<>><<<<>>><>>><<>>><<>><<<>><>>><<<<>>><<<<><<<>>><<<>>>><<><><<<><<>><<>>>><<<>>>><<<<><<<<>>><>>>><<><<<<>><<<<>>>><<<<>>>><>>><<<<>><<<>>>><<<<>>><<<<>>>><<<<><>>>><<><>><<><<><<>>><<<<><<>>><<>>>><<<>><<<<><<<>>>><<<><>><<<<>>><<<>>><<<><<>><<<<>><<<><<>>>><><>>><<>>>><<>><>>>><<<><<>>>><<>>><<<<>><<<>>>><<>>>><<<<>>>><<<<>>><>><<<>>><<<<><<<>>>><<<>>>><>><<<<>>><<>><<<>>>><<>>><>>><<<<>><>>><<<>>><<<<><><<<>><<<<>>>><<<<>>>><<<<>>><>><><<>>>><<<>>>><<<>><<<>>><<>><<<<>><><<><<>>>><<>>><<<><<>>>><<<<>>>><<<>><>>><><<<><<<>>><<><<<<>>>><<<<>>><<>><<>>><<<<><<>>>><<>>>><<<>>>><<><>><<<>><<<<>>><<<<>>><<>><<<<>>>><<<<>>>><<<<><<<<>>>><<>>>><<<>>><<>><>>><<>>><<>>><<<>>>><<<>>><><>>>><<>><>>>><<<>><>>><<<><<>>><<><<<>><>>><<>><>>><<>>>><<<<>><<>>>><>><<<><<<<>>><<<><>>><<<<>>>><<<>><<<<><>>><<<<>><>>><<<>>>><<>>>><<<<>>><<><<<<>>><<<>>><><<<>>>><<><<<>>>><<>><>>>><<>>>><><<<><<>>>><<>><<>>><>><><<<>>>><<>>><<<<>><>>>><<<>><<><>>><<><<<>>>><<><<>>><>>>><<>>>><<<><<>>><>>><>><>><<<>>>><<<><<<>><<<<>><<<>><<>>>><>>>><<<<>>>><<>>><<><<<>>><<<<>><<<<>>>><<>><<<<><<><>>>><<<>>>><<>>><><<<<>><>>><<<<><<<<>>>><<<<>><>><>>>><<<<>>><<>>><>>>><<>>><<<<>>><><<<>>>><<>>><<<<>><<<><>>><><<><<><<><<<><<<>>>><<<>>>><<<<><>>>><>>><>><<>>><<<><>><<<<>>>><><<<>>><<<>><<><<<>>><<><<<>><<<><<<<>>><<>>>><<>>><<<><<<>><<><><<>>>><<>><<<>>>><<>>><<<<>>><<<<>>><<<>>>><<<<>><<<>>><<>>><<>>><>>><<>>><<>>><<<<>><<<<><<>><<<<>>>><>>><<>>>><<<<>><>><>>>><><>>>><>>>><<<>><<<<>>>><<<>>><<>>><<<<><<>><<<<>>><<<>>>><>>>><<<>><<<><<<>><<<<>><<><<>>><><<<<>><>><<>>><><<<<>>><<>>><<<><<>><<>><>><<>><<>>>><<<><<<<>>>><<<>>><<>><<<<>>><<<<>><><<<<>>>><<<<><>><>>>><<<><<>>>><<>>><<>><<<<>>>><><>><<<><>>>><<<>>><<<>>><>><<>>>><<<><<<><<<<><<>><<<>>><<<<>><<<<><<<<><<<>>>><<<><><<<<>><<><<>>> diff --git a/src/day17.lisp b/src/day17.lisp @@ -0,0 +1,116 @@ +(defpackage #:adventofcode2022/day17 + (:use #:cl #:adventofcode2022) + (:import-from #:alexandria + #:define-constant)) +(in-package #:adventofcode2022/day17) + +(defclass rock () + ((shape :initarg :shape + :accessor shape))) + +(defmethod width ((r rock)) + (array-dimension (shape r) 1)) + +(defmethod height ((r rock)) + (array-dimension (shape r) 0)) + +(defparameter *rocks* + (vector (make-instance 'rock + :shape #2A((1 1 1 1))) + (make-instance 'rock + :shape #2A((0 1 0) + (1 1 1) + (0 1 0))) + (make-instance 'rock + :shape #2A((0 0 1) + (0 0 1) + (1 1 1))) + (make-instance 'rock + :shape #2A((1) + (1) + (1) + (1))) + (make-instance 'rock + :shape #2A((1 1) + (1 1))))) + +(defclass cave () + ((positions :initform (make-hash-table :test 'equal) + :reader positions) + (width :initarg :width + :reader width) + (tip-y :initform -1 + :accessor tip-y))) + +(defmethod put-rock ((c cave) (r rock) x y) + (loop for rock-y below (height r) + do (loop for rock-x below (width r) + for cave-x = (+ rock-x x) + for cave-y = (- y rock-y) + do (when (= (aref (shape r) rock-y rock-x) 1) + (when (> cave-y (tip-y c)) + (setf (tip-y c) cave-y)) + (setf (gethash (list cave-x cave-y) + (positions c)) + t))))) + +(defmethod rock-collides-p ((c cave) (r rock) x y) + (when (or (< x 0) + (> (+ x (width r)) (width c)) + (< (- y (1- (height r))) 0)) + (return-from rock-collides-p t)) + (loop named outer + for rock-y below (height r) + do (loop for rock-x below (width r) + for cave-x = (+ rock-x x) + for cave-y = (- y rock-y) + when (and (= (aref (shape r) rock-y rock-x) 1) + (gethash (list cave-x cave-y) + (positions c))) + do (return-from outer t)))) + +(defmethod print-cave ((c cave)) + (loop for y from (tip-y c) downto 0 + do (loop initially (format t "|") + for x below (width c) + do (format t (if (gethash (list x y) (positions c)) + "#" ".")) + finally (format t "|~%")) + finally (loop initially (format t "+") + for x below (width c) + do (format t "-") + finally (format t "+~%")))) + +(defun task1 (input) + (loop with cave = (make-instance 'cave :width 7) + with dirs = (car input) + with round = 0 + for i below 2022 + for rock = (aref *rocks* (mod i (length *rocks*))) + do (loop with rock-x = 2 + with rock-y = (+ (tip-y cave) 3 (height rock)) + for dir = (aref dirs (mod round (length dirs))) + do (incf round) + do (cond + ((char= dir #\<) + (unless (rock-collides-p cave rock + (1- rock-x) + rock-y) + (decf rock-x))) + ((char= dir #\>) + (unless (rock-collides-p cave rock + (1+ rock-x) + rock-y) + (incf rock-x)))) + if (rock-collides-p cave rock rock-x (1- rock-y)) + do (put-rock cave rock rock-x rock-y) + and return nil + else + do (decf rock-y)) + ;;do (print-cave cave) + finally (return (1+ (tip-y cave))))) + +(define-day 17 + () + #'task1 + nil) diff --git a/t/day17.lisp b/t/day17.lisp @@ -0,0 +1,10 @@ +(in-package #:adventofcode2022/test) + +(define-constant +testdata-day17+ ">>><<><>><<<>><>>><<<>>><<<><<<>><>><<>>" + :test 'equal) + +(def-test day17-task1 () + (is-true + (= 3068 + (run-task 17 1 + (make-string-input-stream +testdata-day17+)))))