advent-of-code-2023

My solutions to AoC 2023
git clone git://git.entf.net/advent-of-code-2023
Log | Files | Refs

commit b8b20282833063ad94b1908cc8306e85aedf677e
parent be76554e85d7a77b4e6a473b9761762daa0ee338
Author: Lukas Henkel <lh@entf.net>
Date:   Wed, 13 Dec 2023 06:52:42 +0100

Refactoring

Diffstat:
Msrc/day-13.lisp | 57+++++++++++++++++++++++++++++----------------------------
1 file changed, 29 insertions(+), 28 deletions(-)

diff --git a/src/day-13.lisp b/src/day-13.lisp @@ -3,36 +3,37 @@ (:export #:day-13)) (in-package #:aoc/day-13) -(defun find-vertical-point-of-reflection (map) - (loop with width = (input-map-width map) - with height = (input-map-height map) - for reflection-x from 0 below (1- width) - for reflection-length = (min (1+ reflection-x) - (- width reflection-x 1)) - when (loop repeat reflection-length - for x-1 downfrom reflection-x - for x-2 from (1+ reflection-x) - always (loop for y from 0 below height - always (char= (map-cell map (cons x-1 y)) - (map-cell map (cons x-2 y))))) - do (return (1+ reflection-x)))) +(defun reflection-finding-properties (map type) + (ecase type + (:vertical + (values (input-map-width map) + (input-map-height map) + (lambda (primary secondary) + (cons primary secondary)))) + (:horizontal + (values (input-map-height map) + (input-map-width map) + (lambda (primary secondary) + (cons secondary primary)))))) -(defun find-horizontal-point-of-reflection (map) - (loop with width = (input-map-width map) - with height = (input-map-height map) - for reflection-y from 0 below (1- height) - for reflection-length = (min (1+ reflection-y) - (- height reflection-y 1)) - when (loop repeat reflection-length - for y-1 downfrom reflection-y - for y-2 from (1+ reflection-y) - always (loop for x from 0 below width - always (char= (map-cell map (cons x y-1)) - (map-cell map (cons x y-2))))) - do (return (1+ reflection-y)))) +(defun find-point-of-reflection (map type) + (multiple-value-bind (primary-axis-length secondary-axis-length make-point) + (reflection-finding-properties map type) + (loop for reflection-point from 0 below (1- primary-axis-length) + for reflection-length = (min (1+ reflection-point) + (- primary-axis-length reflection-point 1)) + when (loop repeat reflection-length + for compare-1 downfrom reflection-point + for compare-2 from (1+ reflection-point) + always (loop for secondary-axis-point from 0 below secondary-axis-length + for point-1 = (funcall make-point compare-1 secondary-axis-point) + for point-2 = (funcall make-point compare-2 secondary-axis-point) + always (char= (map-cell map point-1) + (map-cell map point-2)))) + do (return (1+ reflection-point))))) (defun day-13 (input) (loop for map = (make-map input) while map - sum (or (find-vertical-point-of-reflection map) - (* (find-horizontal-point-of-reflection map) 100)))) + sum (or (find-point-of-reflection map :vertical) + (* (find-point-of-reflection map :horizontal) 100))))