refactored and redesign data structures runs now in 2s

This commit is contained in:
Peter Tillemans 2024-12-07 00:40:24 +01:00
parent 48314e2421
commit 8d52daa00c

View file

@ -1,4 +1,4 @@
(declaim (optimize (speed 3) (debug 0) (safety 0))) ;(declaim (optimize (speed 3) (debug 0) (safety 0)))
(defpackage :aoc/2024/06 (defpackage :aoc/2024/06
(:use :cl :aoc :alexandria :trivia) (:use :cl :aoc :alexandria :trivia)
@ -12,15 +12,24 @@
(in-package :aoc/2024/06) (in-package :aoc/2024/06)
(defun list-of-strings-p (list)
"Return t if LIST is non nil and contains only strings."
(and (consp list)
(every #'stringp list)))
(deftype list-of-strings ()
`(satisfies list-of-strings-p))
(declaim (ftype (function (list-of-strings) (simple-array standard-char (* *)))))
(defun parse-input (lines) (defun parse-input (lines)
(let ((l (length lines))) (let ((l (length lines)))
(make-array (list l l) (make-array (list l l)
:initial-contents (mapcar (lambda (s) (coerce s 'list)) lines)))) :initial-contents (mapcar (lambda (s) (coerce s 'list)) lines))))
(declaim (type list-of-strings input-text))
(defparameter input-text (test-input 2024 6)) (defparameter input-text (test-input 2024 6))
(declaim (type (simple-array standard-char (* *)) input-data))
(defparameter input-data (defparameter input-data
(parse-input (test-input 2024 6))) (parse-input (test-input 2024 6)))
@ -35,88 +44,104 @@
#......... #.........
......#... ......#...
")) "))
(defparameter sample-data (defparameter sample-data
(parse-input sample-text)) (parse-input sample-text))
(deftype direction () '(member :north :east :south :west))
(defstruct guard
(x 0 :type fixnum)
(y 0 :type fixnum)
(dir :north :type direction))
(defun make-guard (pos direction)
(list pos direction))
(defun guardp (labo pos) (defun blockedp (labo g &optional extra-block)
(eq #\^ (aref labo (second pos) (first pos)))) (or (eq #\# (aref labo (guard-y g) (guard-x g)))
(and
(defun blockedp (labo pos) extra-block
(eq #\# (aref labo (second pos) (first pos)))) (eq (guard-x g) (car extra-block))
(eq (guard-y g) (cdr extra-block)))))
(defun find-guard (labo) (defun find-guard (labo)
(make-guard (declare (type (array t (* *)) labo))
(loop for x from 0 to (1- (array-dimension labo 0)) (first (loop for x from 0 to (1- (array-dimension labo 0))
for guard-pos = (loop for y from 0 to (1- (array-dimension labo 1)) append (loop for y from 0 to (1- (array-dimension labo 1))
for pos = (list x y) if (eq #\^ (aref labo y x))
if (guardp labo pos) collect (make-guard :x x :y y :dir :north)))))
return pos)
if guard-pos
return guard-pos)
:north))
(defun move (pos direction) (defun move (g)
(destructuring-bind (x y) pos (declare (type guard g))
(case direction (let ((dir (guard-dir g)))
(:north (list x (1- y))) (case dir
(:east (list (1+ x) y)) (:north (make-guard :x (guard-x g) :y (1- (guard-y g)) :dir dir))
(:south (list x (1+ y))) (:east (make-guard :x (1+ (guard-x g)) :y (guard-y g) :dir dir))
(:west (list (1- x) y))))) (:south (make-guard :x (guard-x g) :y (1+ (guard-y g)) :dir dir))
(:west (make-guard :x (1- (guard-x g)) :y (guard-y g) :dir dir)))))
(defun out-labop (labo pos) (defun out-labop (labo g)
(destructuring-bind (x y) pos (declare (type (array t (* *)) labo)
(or (type (or null guard) g))
(< x 0) (not
(< y 0) (and
(>= x (array-dimension labo 1)) g
(>= y (array-dimension labo 1))))) (< -1 (guard-x g) (array-dimension labo 1))
(< -1 (guard-y g) (array-dimension labo 0)))))
(defun guard-rotate (guard)
(let ((direction (second guard))) (defun guard-rotate (g)
(make-guard (declare (type guard g))
(first guard) (make-guard :x (guard-x g) :y (guard-y g)
(case direction :dir (case (guard-dir g)
(:north :east) (:north :east)
(:east :south) (:east :south)
(:south :west) (:south :west)
(:west :north))))) (:west :north))))
(defun guard-move (labo guard &optional (extra-block nil)) (defun guard-move (labo guard &optional (extra-block nil))
(destructuring-bind (pos dir) guard (let ((new-guard (move guard)))
(let ((new-pos (move pos dir)))
(cond (cond
((out-labop labo new-pos) nil) ((out-labop labo new-guard) nil)
((or (blockedp labo new-pos) ((blockedp labo new-guard extra-block)
(equal new-pos extra-block))
(guard-move labo (guard-rotate guard) extra-block)) (guard-move labo (guard-rotate guard) extra-block))
(t (make-guard new-pos dir)))))) (t new-guard))))
(defun guard-path (labo guard &optional (path nil) (extra-block nil)) (defun guard-hash (g)
(declare (type (or null guard) g))
(if g
(+ (* 10000 (guard-y g))
(* 10 (guard-x g))
(case (guard-dir g)
(:north 0)
(:east 1)
(:south 2)
(:west 3)))
0))
(defun hash-to-pos (h)
(cons
(floor (/ h 10000))
(mod (floor (/ h 10)) 1000)
))
(defun guard-path (labo guard &optional (path (make-hash-table :size 10000)) (extra-block nil))
(let ((hash (guard-hash guard)))
(cond (cond
((member guard path :test #'equal) nil) ((not guard) (remove-duplicates (loop for k being the hash-keys of path collect (floor (/ k 10)))))
((emptyp guard) (reverse path)) ((gethash hash path) nil)
(t (guard-path labo (guard-move labo guard extra-block) (push guard path) extra-block)))) (t (progn
(setf (gethash hash path) 1)
(defun number-squares-covered-by-guard (labo) (guard-path labo (guard-move labo guard extra-block) path extra-block))))))
(let ((squares (mapcar #'first (guard-path labo (find-guard labo)))))
(length (remove-duplicates squares :test #'equal))))
(defun part1 (data) (defun part1 (data)
(format nil "~A" (number-squares-covered-by-guard data))) (format nil "~A" (length (guard-path data (find-guard data)))))
(defun looping-blocks (labo) (defun looping-blocks (labo)
(let* ((g (find-guard labo)) (let* ((g (find-guard labo))
(route (guard-path labo g))) (route (guard-path labo g)))
(remove-duplicates
(loop for s in route (loop for s in route
for b = (first s) for b = (cons (mod s 1000) (floor (/ s 1000)))
unless (guard-path labo g '() b) unless (guard-path labo g (make-hash-table :size 10000) b)
collect b) collect b)))
:test #'equal)))
(defun part2 (data) (defun part2 (data)
(let* ((lb (looping-blocks data))) (let* ((lb (looping-blocks data)))