Day 8: Resonant Collinearity
Megathread guidelines
- Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
- You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL
FAQ
- What is this?: Here is a post with a large amount of details: https://programming.dev/post/6637268
- Where do I participate?: https://adventofcode.com/
- Is there a leaderboard for the community?: We have a programming.dev leaderboard with the info on how to join in this post: https://programming.dev/post/6631465
You are viewing a single thread.
View all comments 2 points
*
Lisp
Could probably just write points right to the results instead of to an intermediate list, but it runs instantly, so my motivation to do so was low.
Code
(defun p1-process-line (line)
(to-symbols line 'advt2024-d8))
(defun count-results (results)
(loop for i from 0 below (array-total-size results)
count (row-major-aref results i)))
(defun place-annode (pos results)
(let ((x (first pos)) (y (second pos)))
(when (in-map results x y)
(setf (aref results y x) t))))
(defun create-annodes-p1 (x1 y1 x2 y2)
(let ((delta-x (- x2 x1)) (delta-y (- y2 y1)))
(list (list (- x1 delta-x) (- y1 delta-y)) (list (+ x2 delta-x) (+ y2 delta-y)))))
(defun place-annodes (positions results create-annodes)
(when positions
(loop with a = (car positions)
with x1 = (first a)
with y1 = (second a)
for b in (cdr positions)
for ans = (funcall create-annodes x1 y1 (first b) (second b))
do (dolist (a ans) (place-annode a results)))
(place-annodes (cdr positions) results create-annodes)))
(defun place-all-annodes (xmits map &optional (create-annodes #'create-annodes-p1))
(let ((results (make-array (array-dimensions map) :element-type 'boolean :initial-element nil)))
(loop for k being the hash-key of xmits
do (place-annodes (gethash k xmits) results create-annodes))
results))
(defun find-transmitters (map)
"look throught the map and record where the transmitters are in a hash map"
(let ((h (make-hash-table)))
(destructuring-bind (rows cols) (array-dimensions map)
(loop for j from 0 below rows
do (loop for i from 0 below cols
for v = (aref map j i)
unless (eql v '|.|)
do (push (list i j) (gethash v h))
)))
h))
(defun run-p1 (file)
(let* ((map (list-to-2d-array (read-file file #'p1-process-line))))
(count-results (place-all-annodes (find-transmitters map) map))
))
(defun create-annodes-2 (x1 y1 x2 y2 map)
(destructuring-bind (rows cols) (array-dimensions map)
(let* ((m (/ (- y2 y1) (- x2 x1) ))
(b (- y2 (* m x2))))
(loop for x from 0 below cols
for y = (+ b (* x m))
for r = (nth-value 1 (floor y))
when (and (= r 0) (>= y 0) (< y rows))
collect (list x y)))))
(defun run-p2 (file)
(let* ((map (list-to-2d-array (read-file file #'p1-process-line))))
(count-results (place-all-annodes (find-transmitters map) map
(lambda (x1 y1 x2 y2)
(create-annodes-2 x1 y1 x2 y2 map))))))