this post was submitted on 08 Dec 2024
23 points (96.0% liked)

Advent Of Code

982 readers
61 users here now

An unofficial home for the advent of code community on programming.dev!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

AoC 2024

Solution Threads

M T W T F S S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 1 year ago
MODERATORS
 

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

you are viewing a single comment's thread
view the rest of the comments
[โ€“] [email protected] 2 points 2 weeks ago* (last edited 2 weeks ago)

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))))))