aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2023-01-06 23:10:50 +0100
committerOscar Najera <hi@oscarnajera.com>2023-01-06 23:29:59 +0100
commitb7ecf7861469c5eb8efccf82c449d60aae66b699 (patch)
tree2095a0bbb1c788c976b78004f469724227b6acbf /AoC2022
parente268ea7f1238e82c77dd46df7f438e94f118c1d1 (diff)
downloadscratch-b7ecf7861469c5eb8efccf82c449d60aae66b699.tar.gz
scratch-b7ecf7861469c5eb8efccf82c449d60aae66b699.tar.bz2
scratch-b7ecf7861469c5eb8efccf82c449d60aae66b699.zip
Some cleanup
Diffstat (limited to 'AoC2022')
-rw-r--r--AoC2022/17/solver.lisp36
1 files 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)))))