Created
December 3, 2021 20:46
-
-
Save commander-trashdin/232b331a6a8697e152677bcd7b6375cd to your computer and use it in GitHub Desktop.
AoC, day 3, parts 1&2, Common Lisp, really sloppy work on my part, so please ignore this.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defun update-maxs (line maxs) | |
| (loop :for ch :across line | |
| :for i :from 0 | |
| :when (char= ch #\1) | |
| :do (incf (aref maxs i)))) | |
| (defun day3.1 (path) | |
| (with-open-file (stream path :direction :input | |
| :if-does-not-exist :error) | |
| (let ((firstline (read-line stream nil nil))) | |
| (when firstline | |
| (let* ((l (length firstline)) | |
| (maxs (make-array l :element-type 'fixnum)) | |
| (count 1)) | |
| (update-maxs firstline maxs) | |
| (loop :for line := (read-line stream nil nil) | |
| :while line | |
| :do (update-maxs line maxs) | |
| (incf count)) | |
| (let ((gamma-rate 0) | |
| (eps-rate 0)) | |
| (loop :for num :across maxs | |
| :for i :from (- l 1) :downto 0 | |
| :do (if (> (- count num) num) | |
| (setf (ldb (byte 1 i) gamma-rate) 0 | |
| (ldb (byte 1 i) eps-rate) 1) | |
| (setf (ldb (byte 1 i) gamma-rate) 1 | |
| (ldb (byte 1 i) eps-rate) 0))) | |
| (* gamma-rate eps-rate))))))) | |
| (defun most-common-bit (array i) | |
| (let* ((ones 0)) | |
| (loop :for line :across array | |
| :when (char= (aref line i) #\1) | |
| :do (incf ones)) | |
| (if (> (- (length array) ones) ones) | |
| #\0 | |
| #\1))) | |
| (defun least-common-bit (array i) | |
| (let* ((zeros 0)) | |
| (loop :for line :across array | |
| :when (char= (aref line i) #\0) | |
| :do (incf zeros)) | |
| (if (>= (- (length array) zeros) zeros) | |
| #\0 | |
| #\1))) | |
| (defun read-bytes (path) | |
| (with-open-file (stream path :direction :input | |
| :if-does-not-exist :error) | |
| (let ((res (make-array 0 :adjustable t :fill-pointer 0))) | |
| (loop :for line := (read-line stream nil nil) | |
| :while line | |
| :do (vector-push-extend line res)) | |
| res))) | |
| (defun filter-bits (array i &key type) | |
| (ecase type | |
| (:1 (let ((common (most-common-bit array i))) | |
| (remove-if-not | |
| (lambda (x) | |
| (char= (aref x i) | |
| common)) | |
| array))) | |
| (:0 (let ((uncommon (least-common-bit array i))) | |
| (remove-if-not | |
| (lambda (x) | |
| (char= (aref x i) | |
| uncommon)) | |
| array))))) | |
| (defun day3.2 (path) | |
| (let* ((array (read-bytes path)) | |
| (ones (filter-bits array 0 :type :1)) | |
| (zeros (filter-bits array 0 :type :0)) | |
| (l (length (aref array 0)))) | |
| (loop :for i :from 1 :below l | |
| :for new := (filter-bits ones i :type :1) | |
| :when (/= 0 (length new)) | |
| :do (setf ones new)) | |
| (loop :for i :from 1 :below l | |
| :for new := (filter-bits zeros i :type :0) | |
| :when (/= 0 (length new)) | |
| :do (setf zeros new)) | |
| (* (parse-integer (aref ones 0) :radix 2) | |
| (parse-integer (aref zeros 0) :radix 2)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment