-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.clj
139 lines (115 loc) · 3.5 KB
/
core.clj
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
(ns _2021.three.core
(:require [util :refer [input]]))
(comment
(input "example.txt")
(input "input.txt")
)
(defn counts-to-dec [acc pred]
(->> acc
(map pred)
(apply str)
(#(Integer/parseInt % 2)))
)
(defn acc-bit-diff
"Converts a list of binary strs into a list of diffs by bit."
[xs]
(let [len (->> xs first count)]
(reduce (fn [acc val]
(->> acc
(map-indexed
(fn [i acc-v]
(let [bit (nth val i)]
(case bit
\0 (dec acc-v)
\1 (inc acc-v)))))
vec))
(vec (repeat len 0))
xs)))
(defn most-common-bits [xs]
(-> xs
acc-bit-diff
(counts-to-dec #(cond
(> % 0) 1
(#{0} %) 1
:else 0))))
(defn least-common-bits [xs]
(-> xs
acc-bit-diff
(counts-to-dec #(cond
(> % 0) 0
(#{0} %) 0
:else 1))))
(comment
(counts-to-dec [1 0 1 1 0] #(if (> % 0) 1 0))
(counts-to-dec [1 0 1 1 0] #(if (> % 0) 0 1))
(Integer/parseInt "10110" 2)
(acc-bit-diff (input "input.txt"))
(acc-bit-diff (input "example.txt"))
;; part 1
(let [acc (acc-bit-diff (input "example.txt"))
gamma (counts-to-dec acc #(if (> % 0) 1 0))
epsilon (counts-to-dec acc #(if (> % 0) 0 1))]
(* gamma epsilon))
(let [f "example.txt"
gamma (most-common-bits (input f))
epsilon (least-common-bits (input f))]
(* gamma epsilon)))
;; part 2
(defn diff-bit-n [xs n]
(reduce
(fn [acc val]
(let [bit (nth val n)]
(case bit
\0 (dec acc)
\1 (inc acc))))
0
xs))
(defn bit-crit [type xs n]
(-> (diff-bit-n xs n)
((fn [diff]
(case type
:oxygen (cond
(> diff 0) \1
(#{0} diff) \1
(< diff 0) \0)
:co2 (cond
(> diff 0) \0
(#{0} diff) \0
(< diff 0) \1))))))
(defn rating [type xs]
(loop [remaining xs n 0]
(if (or (< (count remaining) 2)
(> n (count (first remaining))))
(first remaining)
(let [bit-criteria (bit-crit type remaining n)
filtered
(->> remaining
(filter (fn [bin]
(#{bit-criteria} (nth bin n)))))]
(recur filtered (inc n))))))
(defn life-support-rating [xs]
(let [rating-dec (fn [type xs] (-> (rating type xs) (Integer/parseInt 2)))]
(* (rating-dec :oxygen xs) (rating-dec :co2 xs)))
)
(comment
(input "example.txt")
(acc-bit-diff (input "example.txt"))
(diff-bit-n (input "example.txt") 1)
(bit-crit :oxygen (input "example.txt") 0)
(bit-crit :oxygen (input "example.txt") 1)
(bit-crit :oxygen (input "example.txt") 2)
(bit-crit :oxygen (input "example.txt") 3)
(bit-crit :oxygen (input "example.txt") 4)
(bit-crit :co2 (input "example.txt") 0)
(bit-crit :co2 (input "example.txt") 1)
(bit-crit :co2 (input "example.txt") 2)
(bit-crit :co2 (input "example.txt") 3)
(bit-crit :co2 (input "example.txt") 4)
(nth "00101" 2)
(rating :oxygen (input "example.txt"))
(rating :co2 (input "example.txt"))
(rating :oxygen (input "input.txt"))
(rating :co2 (input "input.txt"))
(life-support-rating (input "example.txt"))
(life-support-rating (input "input.txt"))
)