jordan dashel 3 years ago
parent
commit
296ee60e46
2 changed files with 1129 additions and 0 deletions
  1. 1000 0
      3/input.txt
  2. 129 0
      3/submarine.lisp

+ 1000 - 0
3/input.txt

@@ -0,0 +1,1000 @@
+001110000001
+010100101000
+101101010010
+010111101010
+100011100110
+110100001011
+100010001100
+011110100110
+110011110000
+111010011001
+111100100011
+110101101010
+101001111100
+001110101110
+011100001110
+011000101101
+101100001000
+001010000111
+001100101001
+100001000011
+010010111011
+010101101010
+100111100001
+001011101000
+001110100100
+110001001011
+001101110111
+101100100010
+100110001111
+010111011111
+011111111101
+011111110011
+111011000001
+001000011110
+111001100001
+101111101100
+111000001011
+101110010111
+001111110001
+011110000010
+100101011101
+111000100010
+000110111100
+111010111100
+001110100011
+100111001100
+011111100101
+000010000110
+111111010011
+011000010011
+010101011011
+111110010000
+001100110110
+001111010100
+000100000100
+001100001011
+010010110010
+010010101101
+001111010101
+001111011000
+100101100000
+100101111100
+010010100011
+010001011101
+000101100010
+001101001100
+000001110111
+101101010000
+010100101101
+101011110100
+101010100111
+101001100100
+101001000010
+110110101110
+010010000111
+100001110110
+101011000010
+101000101000
+001101001011
+000011100010
+101111011101
+101100101110
+011001011101
+001101100110
+110101000011
+101001101100
+110000111111
+011000110010
+001101000011
+010001100111
+000101010110
+111100011100
+001011100011
+011110110100
+101000011101
+000010111010
+011011011101
+010100101110
+100110101011
+100110111001
+110000001010
+111000001110
+001110100110
+111010011011
+100000011000
+101010101110
+011000000001
+011011001011
+100110011011
+010101010000
+101111001101
+111100111010
+011110101111
+011100110000
+011010000011
+011010101011
+010110101011
+000000101010
+011101100101
+001001111111
+001000011000
+101001010101
+001010000000
+110110010101
+011100101101
+111010110011
+011000001101
+000000100000
+001110101111
+111010100011
+110101111101
+101001100101
+000111011011
+111011101011
+100110001101
+111100110000
+100000100110
+111100000111
+010011100101
+110011000000
+101001000100
+101001000110
+010111101000
+111100110100
+100111011100
+010000001011
+010100100001
+010100000100
+111000101100
+100100000101
+111111010000
+010101111010
+100000001011
+101101001000
+011100000101
+011001101011
+110100010101
+011010100010
+110110110010
+100001010000
+000100100111
+001111110011
+101111010000
+000110011101
+111110000101
+000101010011
+001011111101
+110111001111
+000011111110
+011111111000
+000100101100
+100100010100
+001011111110
+101000011000
+001011001000
+111110000111
+000100000011
+100001100001
+100001111010
+101001100111
+110010100110
+001111001001
+001111111011
+101100001111
+011100010111
+100001001001
+101101001011
+110110011010
+001110011110
+100100101000
+110110001111
+110011001101
+011011010100
+000011101010
+100000011010
+001010111000
+101001001100
+101100111101
+011010111000
+000100001001
+101101100001
+010010111101
+101110101001
+000100001101
+111101111000
+110110110111
+010101011010
+000010100100
+010000011100
+011100100110
+010001110000
+001010011000
+011101100111
+011001111001
+001101110000
+011010111010
+010100000101
+001011010110
+110011101111
+101010001100
+111111101000
+111001110000
+100010001000
+111101110001
+011001110100
+111010111000
+110010110010
+100100001001
+000000100100
+100010111111
+011001000010
+101110001001
+001101101011
+000111000101
+110000011000
+110010011111
+001010011110
+110111001110
+001111110010
+111011111110
+100010101101
+000010101000
+001100001100
+011011111010
+111110001010
+110101100111
+110111011011
+101110111000
+010001001011
+101011100101
+011100000000
+011110000110
+111001111001
+101111110111
+110110100110
+011111010111
+010110000100
+011011101100
+010011111001
+101001011100
+010000010101
+011110011001
+100011101111
+010000101111
+001101011001
+011100011100
+011111010101
+101011011001
+011110011011
+001010101111
+110000000100
+010010011010
+010110000110
+011111100001
+011011000110
+101011010011
+011001101001
+001100001001
+010011001011
+111111110110
+000111001011
+110011111100
+010010000001
+001110111000
+000110010101
+101011100111
+000101010111
+101101111011
+111100000010
+001110011111
+001100111010
+001010100011
+000000011000
+101100010101
+100011110111
+001000110011
+100010110010
+111100110101
+001010011010
+000000100011
+001001101010
+000010000001
+111001000010
+110111111101
+101001110011
+010001011000
+100001010101
+101111101111
+000011000111
+101110000101
+011000111110
+111010010000
+100111001001
+011111111100
+111111001011
+011101110101
+001100101010
+001111101000
+111101011111
+011001000111
+011101110010
+000011100000
+110101101011
+011110001010
+101001100001
+001101110110
+100101001100
+111011100101
+001101100100
+010010110001
+001000101111
+100110110001
+111001100110
+101110111101
+001010101100
+010011110111
+100000110011
+100101100100
+110111101011
+000111000111
+110100101000
+001110001001
+101110001010
+101110110010
+111110010100
+001110000110
+000011100100
+000101000010
+000011011100
+011111000110
+110011010111
+100101001111
+110101000010
+101011001101
+111110101001
+010011111000
+101111011011
+101111100100
+000110011001
+100011111001
+001110011100
+011100001001
+110111100001
+111011011010
+111001111011
+001100101000
+011000100000
+100011001110
+011110111001
+011011111110
+001000110010
+110000010001
+010001110110
+100110101100
+100111100101
+000111111000
+001101011011
+000111010110
+010100001101
+001010000101
+110010101101
+001111001111
+000001111001
+101010110111
+100110000100
+001100111011
+000010100001
+000100111111
+100001111111
+000011111000
+101110011010
+001011010001
+001011110001
+010011100110
+000010110010
+001010101001
+110110111100
+100111010110
+101001011001
+000100111110
+101111001010
+110100011111
+010100011100
+111110100011
+000010101100
+100000101010
+111111000000
+111111010110
+001100011011
+110001101001
+101010011110
+101010000010
+000000000111
+000100101110
+101000001011
+100011000001
+000100101101
+001000100000
+110111001011
+111001001111
+110011100110
+100001110100
+010111101100
+011000001010
+100101000000
+001101100001
+110001000111
+010111100011
+000100111101
+010111011101
+000001010111
+011001111110
+110101111001
+100010101001
+010110001010
+111010110001
+000000101001
+010100011110
+000010100010
+100011001000
+100110010111
+001011010100
+011010001010
+000001110101
+010010100000
+010111100101
+010101011101
+110100010010
+101101111100
+100001101101
+100011010110
+011011000101
+011100110101
+010010010100
+101001001110
+001110110010
+010110110101
+100100010101
+000011010100
+010000111000
+101110010011
+101101110111
+110000000010
+001011001010
+000111010011
+100110100010
+001110001111
+100110101110
+010001010010
+111001011001
+111000110111
+010111000001
+001111011010
+001000101010
+111011110001
+100001001110
+001010001011
+000011100001
+101010011101
+000110000110
+100111110010
+010010100001
+010101011000
+010001001100
+011100110100
+000010101010
+001110111001
+001100010011
+000101000101
+100010111000
+111011010011
+100011110100
+110100011000
+000010011111
+011111011001
+011011001111
+111101110000
+101111000110
+100111100000
+111100011101
+001100010000
+001100110101
+011010110100
+010000010100
+111010101111
+000010101101
+010110010110
+110011111110
+100101101001
+111100000000
+110111101100
+100100001000
+100100001011
+111001001011
+011010110011
+001101110011
+000100101001
+001111010011
+011000000110
+111100100110
+010010001001
+110101011000
+011001111111
+100111010101
+111010001000
+001111111001
+001100100110
+110010100010
+011101001000
+110111100000
+011010101000
+000101111010
+101010010110
+100111101010
+000010110000
+100000001100
+101000010000
+010101000011
+100111101100
+101000110110
+010010110110
+111011100011
+111100111011
+100010101111
+011011110111
+100001011110
+110000100101
+011000111101
+010110000001
+100010011110
+001111011100
+110001011110
+111110100101
+001110000111
+110111100011
+110100101010
+010101110001
+110101011101
+110110100101
+110010000100
+100110110100
+010100011101
+111101010101
+010101010011
+100100011000
+101100010110
+001000011111
+010011110001
+100101100001
+101100000101
+000100011010
+100100100011
+000101001111
+110100100101
+001111000010
+100000001110
+110100000001
+001010010000
+010001000110
+101010110001
+100010100001
+111011101101
+011011111011
+010011111010
+111100001001
+001101110010
+111110111011
+101110010100
+001110110111
+110000000110
+000011111100
+001110010000
+000010001001
+101101101110
+010101100110
+010000110000
+101111100011
+101000001111
+001100100111
+111111000010
+110000000101
+110001010100
+010011011000
+110100101111
+110100010100
+101000001000
+111010110111
+101000110001
+111110000110
+001111100000
+100100000110
+101011000100
+100100010110
+110001011001
+011011010010
+001110111011
+010100111111
+010000110100
+111011010001
+001011001110
+110110101011
+100110010000
+001111000100
+101101101011
+110110010110
+010011101110
+000110101000
+001010010111
+101111000010
+001101101111
+111001111100
+101010101011
+111101100010
+110101000110
+011110000111
+010101110100
+100110111100
+010001010101
+001101011110
+110100010001
+100100011001
+101111111111
+001001001111
+011011111000
+101010111100
+000011011110
+111010010001
+111111011011
+111101110111
+100011111000
+110001101101
+001011011000
+001000101011
+010000000101
+011011000100
+100001011011
+001001001101
+000110001101
+000011011000
+000011000001
+000010010111
+011011101001
+110101001111
+011000101010
+011110100101
+100000110101
+011011100110
+110010010010
+011110111011
+000101101010
+001010111110
+100101101111
+111110001100
+010100110000
+110111111100
+010010010000
+010011000001
+100101010011
+001110100010
+110101010010
+100000010110
+001101100111
+010110011011
+010111000101
+100110101101
+010010101010
+000101011100
+101000001101
+110111000010
+101000110010
+010111010001
+100011001011
+111111100010
+001101110101
+011100111111
+110001101111
+011101101010
+001000100011
+011001110111
+100111000110
+111011111111
+001000111001
+011100011110
+110010100011
+111101111101
+011011011110
+100000010000
+000001110100
+110110001000
+111111011010
+011110111010
+111000011100
+100101110010
+011100001100
+010011000111
+011110001101
+000101101000
+001011011010
+011011110000
+111100010010
+000010101111
+011101101111
+001001110000
+110111011100
+101000010001
+100110000110
+000010000101
+001000101110
+001001001001
+011111010000
+111100100101
+010101110111
+010110000011
+110000111011
+100100110111
+100111000001
+111100010011
+100010000100
+111011100000
+010111110100
+101100001011
+000010101001
+111110111101
+001011110010
+110000101110
+000010111110
+110111011001
+110111110100
+001110100001
+001101110001
+010001110011
+110101111000
+000110101100
+111011010100
+000100001100
+110001110101
+100000111110
+100010010111
+100101010001
+011011000111
+111011110110
+001001101001
+010011110101
+111110100111
+000110100001
+110011001110
+101001101110
+100100110100
+000001001001
+011111110100
+010001101110
+100101010111
+110011010100
+011101010010
+100010101011
+011111101101
+101100101000
+111110000001
+101010111010
+101110111001
+101011000111
+110100110010
+101011000001
+111001000001
+001011110000
+000110011010
+101111101001
+101110000000
+011101010001
+110110011101
+110101011111
+101110000001
+111100001111
+000111100101
+010001101101
+000011011010
+011010001111
+111010001101
+101011001001
+111001101001
+001100001101
+111110100010
+000111110101
+010111101110
+010101100000
+100010100011
+000001100001
+111111000110
+001111110100
+000001101100
+011011011100
+011110001011
+001010101000
+101111100110
+111000111011
+100101101000
+100110001010
+010111010110
+110110001101
+010001001111
+000011110011
+010000111110
+101000011001
+111000000100
+011010000111
+010010101110
+001100001111
+000010001000
+001101101001
+110101101110
+110010011011
+000101011000
+101011101000
+010000110101
+110110001010
+110110011110
+011111011100
+001001011011
+110000111100
+110101001100
+100100101110
+001000111100
+111101011001
+011111011010
+011101101100
+100011010000
+001100000001
+001011000111
+011011111100
+001100101101
+110010101111
+011101111100
+011110101101
+110100011010
+101100100001
+000111111100
+001010110010
+110101101100
+111100000101
+110011000011
+110100111010
+011000111010
+101000000101
+101010001000
+000100110111
+110101101000
+101001111101
+010010001010
+110111101001
+010111001000
+110101100011
+111111100000
+100010010010
+010011000100
+010100100110
+101101000001
+001010010001
+111100101110
+110011100001
+101000000111
+111010100110
+111111010111
+110010101011
+001011010000
+001001100110
+011111110110
+000010110100
+110010000111
+111001100011
+101101001110
+011001111101
+100111110111
+000100000111
+010000110011
+000110111111
+011011100001
+101001001011
+110011101001
+011110010010
+001010101011
+110000111001
+010100111110
+011110101110
+011001111100
+111111001100
+011000001111
+000001100100
+101010001011
+000010111001
+000101110111
+101100011111
+101001000111
+111001011100
+111010100111
+111011010010
+101101111010
+111100011001
+101111101101
+001010000110
+100001010111
+011101001101
+010000101001
+011110101011
+011101000111
+000101100001
+010011111011
+101101111110
+001000110100
+011100000110
+100010000110
+011110010011
+000011110000
+100000010011
+011100011010
+111110011110
+000111011000
+000000001000
+001010110101
+110011111010
+001101100000
+001000000110
+110111111011
+001000010110
+001010000011
+101101011010
+011001100101
+001000010111
+100010111010
+011101101001
+101010000111
+001100111111
+111000000111
+001111011111
+000100001010
+101111111000
+010111100001
+010110010111
+111001010011
+001111001011
+000001010101
+100111111101
+010110100100
+011101010110
+110001010010
+010110010101
+010101000101
+100010111011
+001011101010
+011011110001
+100111110001
+000001111101
+111111111011
+000010010110
+101011010000
+010011010011
+110000001111
+010010001100
+100101001011
+101111001100
+111111001110
+010100101001
+010001111110
+011010011100
+111100011110
+010110100010
+100011011001
+001110000100
+011001111000
+110010010001
+010011001110
+011010100111
+010100101100
+101001011011
+110011110001
+100011000000
+000011111101
+000010110110
+001011011110
+010001011110
+001101010010

+ 129 - 0
3/submarine.lisp

@@ -0,0 +1,129 @@
+(defparameter *diagnostics-input* "./input.txt")
+
+(defun read-diagnostics ()
+  "Read input file, returning a list of its binary diagnostics."
+  (let ((input (open *diagnostics-input* :if-does-not-exist nil))
+        (input-diagnostics '()))
+    (when input
+      (loop for line = (read-line input nil)
+            while line do
+            (setq input-diagnostics (cons line input-diagnostics)))
+      (close input))
+
+    (reverse input-diagnostics)))
+
+
+(defun get-zero-one-frequencies (input)
+  "Determine how many bits have a 1 at this input and how many have a 0."
+  (defun all-bits-at-index (index all-readings)
+    (map 'list (lambda (byte) (elt byte index)) all-readings))
+  (loop for i upto (1- (length (car input))) 
+        collecting (count #\0 (all-bits-at-index i input))
+          into zeros
+        collecting (count #\1 (all-bits-at-index i input))
+          into ones 
+        finally (return (list zeros ones))))
+
+(defun calculate-gamma (input)
+  "Calculate the gamma rate."
+  (let* ((frequencies (get-zero-one-frequencies input))
+         (zeros (car frequencies))
+         (ones (cadr frequencies)))
+    (map 'list
+         (lambda (zero one)
+           (if (> zero one) "0" "1"))
+         zeros
+         ones)))
+
+(defun calculate-epsilon (input)
+  "Calculate the epsilon rate."
+  (defun invert (gamma)
+    (loop for i in gamma
+          if (string= i "0")
+            collect "1"
+          else collect "0"))
+  (invert (calculate-gamma input)))
+
+
+(defun print-power-consumption-report (input)
+  (let* ((gamma (calculate-gamma input))
+         (epsilon (calculate-epsilon input))
+         (gamma-string (format nil "~{~A~}" gamma))
+         (epsilon-string (format nil "~{~A~}" epsilon))
+         (gamma-decimal (parse-integer gamma-string :radix 2))
+         (epsilon-decimal (parse-integer epsilon-string :radix 2)))
+    (format t "gamma:   ~{~a~}~%" gamma)
+    (format t "           ~a~%" gamma-decimal)
+    (format t "epsilon: ~{~a~}~%" epsilon)
+    (format t "           ~a~%" epsilon-decimal)
+    (format t "power consumption: ~a~%" (* epsilon-decimal gamma-decimal))))
+
+;; gamma:   001110101111
+;;            943
+;; epsilon: 110001010000
+;;            3152
+;; power consumption:  2972336
+
+
+(defun oxygen-generator-rating (input)
+  "Calculate the oxygen generator rating.
+Determined by having the most bits in the most-common positions."
+  (in-majority-bit-criteria #'remove-if input))
+
+(defun co2-scrubber-rating (input)
+  "Calculate the CO2 scrubber rating.
+Determined by having the fewest bits in the most-common positions."
+  (in-majority-bit-criteria #'remove-if-not input))
+
+
+(defun in-majority-bit-criteria (filter input)
+  "Reduce list to byte contaning the most filtered majority bit criteria."
+  (defun iter-filter (measurements index)
+    (if (= 1 (length measurements))
+        (car measurements)
+        (iter-filter
+         (funcall
+          filter
+          (lambda (x) (in-the-bit-majority? x index measurements)) measurements)
+         (1+ index))))
+  (iter-filter input 0))
+
+(defun in-the-bit-majority? (byte bit-index all-bytes)
+  "Is the bit at bit-index of byte in the more popular group of bits at this index in all-bytes?"
+  (let* ((bit-frequencies (get-zero-one-frequencies all-bytes))
+         (most-popular-bit
+           (if (> (elt (car bit-frequencies) bit-index)
+                   (elt (cadr bit-frequencies) bit-index))
+               #\0
+               #\1))
+         (current-bit (elt byte bit-index)))
+    (char= current-bit most-popular-bit)))
+
+(defun print-life-support-report (input)
+  (let* ((oxygen (oxygen-generator-rating input))
+         (co2 (co2-scrubber-rating input))
+         (oxy-decimal (parse-integer oxygen :radix 2))
+         (co2-decimal (parse-integer co2 :radix 2)))
+    (format t "oxygen:  ~a~%" oxygen)
+    (format t "           ~a~%" oxy-decimal)
+    (format t "co2:     ~a~%" co2)
+    (format t "           ~a~%" co2-decimal)
+    (format t "life support: ~a~%" (* oxy-decimal co2-decimal))))
+
+
+(print-power-consumption-report (read-diagnostics))
+
+;; gamma:   001110101111
+;;            943
+;; epsilon: 110001010000
+;;            3152
+;; power consumption: 2972336
+
+
+(print-life-support-report (read-diagnostics))
+
+;; oxygen:  111000100010
+;;            3618
+;; co2:     001110100011
+;;            931
+;; life support: 3368358