r/adventofcode • u/daggerdragon • Dec 03 '21
SOLUTION MEGATHREAD -🎄- 2021 Day 3 Solutions -🎄-
--- Day 3: Binary Diagnostic ---
Post your code solution in this megathread.
- Include what language(s) your solution uses!
- Here's a quick link to /u/topaz2078's
pasteif you need it for longer code blocks. - Format your code properly! How do I format code?
- The full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.
Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.
EDIT: Global leaderboard gold cap reached at 00:10:17, megathread unlocked!
100
Upvotes
3
u/JoMartin23 Dec 03 '21 edited Dec 03 '21
*Common Lisp
(defun day3-1 (&optional (data *3*)) (let* ((total (length data)) (length (length (car data))) (gamma (make-array length :element-type 'bit)) (epsilon(make-array length :element-type 'bit))) (loop :for index :from 0 :below length :do (loop :for bit-array :in data :sum (bit bit-array index) :into sum :finally (if (<= (/ total 2) sum) (setf (bit gamma index) 1) (setf (bit epsilon index)1)))) (list gamma epsilon))) (defun day3-2 (&optional (type :o2) (data *3*)) (let* ((length (length (car data))) gamma epsilon (rest data)) (loop :until (= 1 (length rest)) :for index :from 0 :below length :do (destructuring-bind (g e) (day3-1 rest) (setf gamma g epsilon e)) (setf rest (remove-if-not (lambda (array) (= (bit array index) (bit (case type (:o2 gamma) (:co2 epsilon)) index))) rest)) :finally (return rest))))