From 8a2465757c5028ca0fab0fe43e39ae459b17f8fb Mon Sep 17 00:00:00 2001 From: 4903000 <4903000@chiru.no> Date: Thu, 3 Dec 2020 11:48:22 +0000 Subject: [PATCH] Day 3 --- aoc20.lisp | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/aoc20.lisp b/aoc20.lisp index a9873bb..819ca1b 100644 --- a/aoc20.lisp +++ b/aoc20.lisp @@ -73,3 +73,45 @@ (let ((data (read-2 file))) (time (progn (print (%solve-2 data #'%solve-2-1)) (print (%solve-2 data #'%solve-2-2)))))) + +(defun read-3 (file) + (with-open-file (stream file) + (let ((temp-map (loop for line = (read-line stream nil) + while line + collect line))) + (make-array (list (length temp-map) (length (first temp-map))) + :initial-contents temp-map + :element-type 'character)))) + +(defun step-slope (position direction map) + (let* ((temp-position (map 'list #'+ direction position)) + (overflow (map 'list #'> temp-position (array-dimensions map)))) + (cond ((first overflow) :bottom) + ((second overflow) + (progn (decf (second temp-position) (second (array-dimensions map))) + temp-position)) + (t temp-position)))) + +(defun solve-3-1 (direction map) + (let ((tree-count 0)) + (labels ((rec (position) + (let ((zero-pos (map 'list #'1- position)) + (new-pos (step-slope position direction map))) + (if (char= (aref map (first zero-pos) (second zero-pos)) + #\#) + (incf tree-count)) + (unless (eq new-pos :bottom) + (rec new-pos))))) + (rec '(1 1)) + tree-count))) + +;; (height width) + +(defun solve-3-2 (slopes map) + (reduce #'* (loop for slope in slopes + collecting (solve-3-1 slope map)))) + +(defun solve-3 (map) + (time (let ((data (read-3 map))) + (print (solve-3-1 '(1 3) data)) + (print (solve-3-2 '((1 1) (1 3) (1 5) (1 7) (2 1)) data)))))