Skip to content

Commit

Permalink
Sync utils and fix compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
iamFIREcracker committed Jan 6, 2025
1 parent d046237 commit 8d400dd
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/2024/day05.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -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))))
Expand Down
2 changes: 1 addition & 1 deletion src/2024/day10.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -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)))))))
Expand Down
2 changes: 1 addition & 1 deletion src/2024/day11.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -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))))))
Expand Down
4 changes: 2 additions & 2 deletions src/2024/day12.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -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)))))
Expand Down Expand Up @@ -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))))
Expand Down
4 changes: 2 additions & 2 deletions src/2024/day14.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
(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))))))
#+#:excluded (parse-input)


(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*))
Expand Down
2 changes: 1 addition & 1 deletion src/2024/day22.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion src/2024/day23.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
11 changes: 5 additions & 6 deletions src/2024/day24.lisp
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
(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)
(declare (ignore _))
(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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/utils.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -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)))


Expand Down
21 changes: 17 additions & 4 deletions vendor/quickutils.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -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
=> #<HASH-TABLE :TEST EQL :COUNT 2 {700AB03913}> ; 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))

Expand Down

0 comments on commit 8d400dd

Please sign in to comment.