master
4903000 2 years ago
parent d06a3008e5
commit c5a0388737

@ -67,3 +67,49 @@
(setup-solution-2 2)
(eval (cons 'progn data))
(reduce #'* (result)))
;;; Day 3
(defun vector+ (&rest vectors)
(labels ((internal (old new)
(map 'vector (lambda (n m) (+ n m)) old new)))
(reduce #'internal vectors)))
(defun read-3 (file)
(with-open-file (stream file)
(loop for line = (read-line stream nil)
while line
collect (map 'vector (lambda (c) (- (char-code c) 48))
line))))
(defun solve-3-1 (data)
(let* ((max (/ (length data) 2))
(sums (reduce #'vector+ data))
(gamma (map 'string (lambda (i) (digit-char (if (< 0 (- i max)) 1 0))) sums))
(epsilon (map 'string (lambda (c) (if (char= c #\1) #\0 #\1)) gamma)))
(* (parse-integer gamma :radix 2)
(parse-integer epsilon :radix 2))))
(defun rec (data acc head bias)
(if data
(rec (cdr data)
(if (= (elt (first data) head) bias)
(list (cons (first data) (first acc)) (second acc))
(list (first acc) (cons (first data) (second acc))))
head bias)
(if (funcall ((lambda () (if (= bias 1) #'< #'>)))
(length (first acc)) (length (second acc)))
(second acc)
(first acc))))
(defun recrec (data head bias)
(let ((new-data (rec data (list nil nil) head bias)))
(if (= (length new-data) 1)
(first new-data)
(recrec new-data (+ 1 head) bias))))
(defun solve-3-2 (data)
(let ((oxygen (map 'string (lambda (n) (digit-char n)) (recrec data 0 1)))
(co2 (map 'string (lambda (n) (digit-char n)) (recrec data 0 0))))
(* (parse-integer oxygen :radix 2)
(parse-integer co2 :radix 2))))

Loading…
Cancel
Save