From 8d400dd051150d17a1e3b9cae42b926870180210 Mon Sep 17 00:00:00 2001 From: Matteo Landi Date: Sun, 5 Jan 2025 14:55:59 +0100 Subject: [PATCH] Sync utils and fix compilation --- src/2024/day05.lisp | 2 +- src/2024/day10.lisp | 2 +- src/2024/day11.lisp | 2 +- src/2024/day12.lisp | 4 ++-- src/2024/day14.lisp | 4 ++-- src/2024/day22.lisp | 2 +- src/2024/day23.lisp | 2 +- src/2024/day24.lisp | 11 +++++------ src/utils.lisp | 2 +- vendor/quickutils.lisp | 21 +++++++++++++++++---- 10 files changed, 32 insertions(+), 20 deletions(-) diff --git a/src/2024/day05.lisp b/src/2024/day05.lisp index 1726f7b..d341ccb 100644 --- a/src/2024/day05.lisp +++ b/src/2024/day05.lisp @@ -3,7 +3,7 @@ (defun parse-input (&optional (strings (uiop:read-file-lines #P"src/2024/day05.txt"))) (destructuring-bind (rules updates) (split-sequence:split-sequence "" strings :test 'equal) - (list (prog1-let (before-than (make-hash-table)) + (list (prog1-let before-than (make-hash-table) (doseq ((a b) (mapcar #'extract-positive-integers rules)) (push b (gethash a before-than)))) (mapcar #'extract-positive-integers updates)))) diff --git a/src/2024/day10.lisp b/src/2024/day10.lisp index 5cc58c2..092ee80 100644 --- a/src/2024/day10.lisp +++ b/src/2024/day10.lisp @@ -3,7 +3,7 @@ (defun parse-input (&optional (strings (uiop:read-file-lines #P"src/2024/day10.txt"))) - (prog1-let (map (make-hash-table :test 'equal)) + (prog1-let map (make-hash-table :test 'equal) (doseq ((i s) (enumerate strings)) (doseq ((j ch) (enumerate s)) (setf (gethash (list i j) map) (- (char-code ch) (char-code #\0))))))) diff --git a/src/2024/day11.lisp b/src/2024/day11.lisp index 49b82b9..c7a9040 100644 --- a/src/2024/day11.lisp +++ b/src/2024/day11.lisp @@ -20,7 +20,7 @@ (let1 curr (make-counter stones) (repeat times (setf curr - (prog1-let (next (make-counter nil)) + (prog1-let next (make-counter nil) (dohash (stone n curr) (dolist (stone1 (change stone)) (incf (gethash stone1 next 0) n)))))) diff --git a/src/2024/day12.lisp b/src/2024/day12.lisp index a55a97c..6c6b6e4 100644 --- a/src/2024/day12.lisp +++ b/src/2024/day12.lisp @@ -2,7 +2,7 @@ (in-package :aoc/2024/12) (defun parse-input (&optional (strings (uiop:read-file-lines #P"src/2024/day12.txt"))) - (prog1-let (map (make-hash-table :test 'equal)) + (prog1-let map (make-hash-table :test 'equal) (doseq ((i s) (enumerate strings)) (doseq ((j ch) (enumerate s)) (setf (gethash (list i j) map) ch))))) @@ -38,7 +38,7 @@ ;; facing the same direction, and if there is, we subtract one from the total ;; sides count. (defun sides (r &aux (fence (fence r))) - (prog1-let (total (length fence)) + (prog1-let total (length fence) (flet ((adjacent? (p1 p2) (= (+ (abs (- (car p1) (car p2))) (abs (- (cadr p1) (cadr p2)))) diff --git a/src/2024/day14.lisp b/src/2024/day14.lisp index 657bd26..c4ea256 100644 --- a/src/2024/day14.lisp +++ b/src/2024/day14.lisp @@ -5,7 +5,7 @@ (defparameter *height* 103) (defun parse-input (&optional (strings (uiop:read-file-lines #P"src/2024/day14.txt"))) - (prog1-let (grid (make-hash-table :test 'equal)) + (prog1-let grid (make-hash-table :test 'equal) (dolist (s strings) (destructuring-bind (x y vx vy) (extract-integers s) (push (list vx vy) (gethash (list x y) grid)))))) @@ -13,7 +13,7 @@ (defun tick (curr) - (prog1-let (next (make-hash-table :test 'equal)) + (prog1-let next (make-hash-table :test 'equal) (dohash ((x y) robots curr) (doseq ((vx vy) robots) (let ((x1 (mod (+ x vx) *width*)) diff --git a/src/2024/day22.lisp b/src/2024/day22.lisp index febd027..993e55a 100644 --- a/src/2024/day22.lisp +++ b/src/2024/day22.lisp @@ -25,7 +25,7 @@ #+#:excluded (prices 123) (defun winning-by-sequence (prices) - (prog1-let (map (make-hash-table :test 'equal)) + (prog1-let map (make-hash-table :test 'equal) (dosublists ((p1 p2 p3 p4 p5) prices) (when (and p1 p2 p3 p4 p5) (let1 sequence (list (- p2 p1) (- p3 p2) (- p4 p3) (- p5 p4)) diff --git a/src/2024/day23.lisp b/src/2024/day23.lisp index d715532..e88fb54 100644 --- a/src/2024/day23.lisp +++ b/src/2024/day23.lisp @@ -2,7 +2,7 @@ (in-package :aoc/2024/23) (defun parse-input (&optional (strings (uiop:read-file-lines #P"src/2024/day23.txt"))) - (prog1-let (edges (make-hash-table :test 'equal)) + (prog1-let edges (make-hash-table :test 'equal) (doseq (s strings) (destructuring-bind (from to) (split-sequence:split-sequence #\- s) (pushnew to (gethash from edges)) diff --git a/src/2024/day24.lisp b/src/2024/day24.lisp index fe35049..8e434f8 100644 --- a/src/2024/day24.lisp +++ b/src/2024/day24.lisp @@ -1,19 +1,18 @@ (defpackage :aoc/2024/24 #.cl-user::*aoc-use*) (in-package :aoc/2024/24) -(declaim (optimize (debug 3))) - +#; (defun parse-input (&optional (strings (uiop:read-file-lines #P"src/2024/day24.txt"))) (destructuring-bind (connections initial-wires) (split-sequence:split-sequence "" strings :test 'equal) (list ;; values - (prog1-let (map (make-hash-table :test 'equal)) + (prog1-let1 map (make-hash-table :test 'equal) (dolist (s connections) (setf (gethash (subseq s 0 3) map) (parse-integer (subseq s 5))))) ;; inputs - (prog1-let (map (make-hash-table :test 'equal)) + (prog1-let map (make-hash-table :test 'equal) (dolist (s initial-wires) (destructuring-bind (in1 gate in2 _ output) (split-sequence:split-sequence #\Space s) @@ -21,7 +20,7 @@ (setf (gethash in1 map) (list in1 gate in2 output) (gethash in2 map) (list in1 gate in2 output) )))) ;; outputs - (prog1-let (map (make-hash-table :test 'equal)) + (prog1-let map (make-hash-table :test 'equal) (dolist (s initial-wires) (destructuring-bind (in1 gate in2 _ output) (split-sequence:split-sequence #\Space s) @@ -140,7 +139,7 @@ (same-expansion? rand12 rand21))))))))) (defun swap (outputs gate1 gate2 gate3 gate4 gate5 gate6 gate7 gate8) - (prog1-let (copy (copy-hash-table outputs)) + (prog1-let1 copy (copy-hash-table outputs) (rotatef (gethash gate1 copy) (gethash gate2 copy)) (rotatef (gethash gate3 copy) diff --git a/src/utils.lisp b/src/utils.lisp index 4651bf2..3be5910 100644 --- a/src/utils.lisp +++ b/src/utils.lisp @@ -112,7 +112,7 @@ (defun make-counter (x &key (test 'eql)) "Returns a HASH-TABLE mapping _unique_ elements of `x` to the number of times they occur in `x`." - (prog1-let (map (make-hash-table :test test)) + (prog1-let map (make-hash-table :test test) (map nil (lambda (e) (incf (gethash e map 0))) x))) diff --git a/vendor/quickutils.lisp b/vendor/quickutils.lisp index f51af0d..a9a73b2 100644 --- a/vendor/quickutils.lisp +++ b/vendor/quickutils.lisp @@ -1355,11 +1355,24 @@ without altering the original behavior. (first args)) - (defmacro prog1-let ((name result-form) &body body) - "Like PROG1, except it lets you bind the result of the `result-form` (i.e., the returned -form) to `name` (via LET) for the scope of `body`. + (defmacro prog1-let (name result-form &body body) + "Evaluates `result-form`, binds its value to `name`, executes `body`, and +finally return `name`. Similar to PROG1 but maintains the binding of +the result throughout `body`'s execution. -Inspired by ActiveSupport: Object#returning +Example: + + (prog1-let x (list 1 2 3) + (setf (first x) 10) ; modifies the list + (print x)) ; prints (10 2 3) + => (10 2 3) ; returns the modified list + + (prog1-let x (make-hash-table) + (setf (gethash 'hello x) t) ; modifies the hash-table + (setf (gethash 'world x) t) ; modifies it again + => # ; returns the initialized hash-table + +Inspired by ActiveSupport's Object#returning from Ruby on Rails: https://weblog.jamisbuck.org/2006/10/27/mining-activesupport-object-returning.html" (prog1-let-expand name result-form body))