From b7ecf7861469c5eb8efccf82c449d60aae66b699 Mon Sep 17 00:00:00 2001 From: Oscar Najera Date: Fri, 6 Jan 2023 23:10:50 +0100 Subject: Some cleanup --- AoC2022/17/solver.lisp | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/AoC2022/17/solver.lisp b/AoC2022/17/solver.lisp index ecabf98..7a77123 100644 --- a/AoC2022/17/solver.lisp +++ b/AoC2022/17/solver.lisp @@ -20,26 +20,22 @@ (list (point 0 0) (point 1 0) (point 0 1) (point 1 1)))) -(defun left (point) (1- point)) -(defun right (point) (1+ point)) -(defun down (point) (- point +chamber-width+)) - (defun no-collision-move (object direction obstacles) (mapcar (lambda (current) (let ((next (funcall (ecase direction - (#\> #'right) - (#\< #'left) - (#\v #'down)) + (#\> #'1+) + (#\< #'1-) + (#\v #'(lambda (p) (- p +chamber-width+)))) current))) - (multiple-value-bind (y2 x2) (coords next) - (if (and (>= y2 0) - ;; no rolling boundaries - (multiple-value-bind (y x) (coords current) - (= 1 (+ (abs (- x x2)) (abs (- y y2))))) - (not (member next obstacles :test #'eq))) - next (return-from no-collision-move object))))) + (if (and (>= next 0) + ;; no rolling boundaries + (multiple-value-bind (y x) (coords current) + (multiple-value-bind (y2 x2) (coords next) + (= 1 (+ (abs (- x x2)) (abs (- y y2)))))) + (not (member next obstacles :test #'eq))) + next (return-from no-collision-move object)))) object)) (fiveam:test moves @@ -80,7 +76,6 @@ (defun solver (drops-left highpoint raise-history obstacles next-rock next-shift) (if (zerop drops-left) raise-history - ;; highpoint (multiple-value-bind (new-obstacles posible-high) (simulate obstacles (place-rock (funcall next-rock) (+ highpoint 3)) @@ -97,8 +92,11 @@ (let ((table (make-hash-table :test #'eq))) (loop for e in lst do (incf (gethash e table 0))) - (loop for k being the hash-key of table using (hash-value v) - collect (cons k v)))) + table)) + +(defun to-alist (table) + (loop for k being the hash-key of table using (hash-value v) + collect (cons k v))) (defun find-period (history max-window) (let ((len (1- (length history)))) @@ -108,8 +106,8 @@ for end from size by size collect (- (aref history (min end len)) (aref history start))) for periods = (occurrences raises) - for test = (and (<= (length periods) 3) - (car (find-if (lambda (f) (< 1 (cdr f))) periods))) + for test = (and (<= (hash-table-count periods) 3) + (car (find-if (lambda (f) (< 1 (cdr f))) (to-alist periods)))) until test finally (return (values size test))))) -- cgit v1.2.3