-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.lisp
49 lines (39 loc) · 1.72 KB
/
3.lisp
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
(defparameter *report* (uiop:read-file-lines "input/day3.txt"))
(defparameter *number-length* (length (car *report*)))
(defun majority-bit (nth report)
(if (>= (count #\1 (mapcar #'(lambda (seq) (elt seq nth))
report))
(/ (length report) 2))
#\1
#\0))
(defun gamma-rate (num-len report)
(parse-integer (coerce (loop for index from 0 below num-len
collect (majority-bit index report))
'string)
:radix 2))
(defun epsilon-rate (num-len gamma-rate)
(logxor gamma-rate
(parse-integer (make-string num-len :initial-element #\1) :radix 2)))
(defun solve-first-part ()
(let ((gamma (gamma-rate *number-length* *report*)))
(* gamma (epsilon-rate *number-length* gamma))))
;; Part II
(defun rating-template (criteria-fn num-len report)
(loop with report = (copy-seq report)
for index from 0 below num-len
do (progn (setf report (remove-if-not
#'(lambda (seq)
(char-equal (funcall criteria-fn index report)
(elt seq index)))
report)))
until (= 1 (length report))
finally (return (parse-integer (car report) :radix 2))))
(defun oxygen-rating (num-len report)
(rating-template #'majority-bit num-len report))
(defun CO2-rating (num-len report)
(flet ((co2-bit-criteria (nth report)
(if (char-equal #\1 (majority-bit nth report)) #\0 #\1)))
(rating-template #'co2-bit-criteria num-len report)))
(defun solve-second-part ()
(* (oxygen-rating *number-length* *report*)
(co2-rating *number-length* *report*)))