-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday04.clj
55 lines (44 loc) · 1.1 KB
/
day04.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
(ns day04
(:require aoc
[clojure.string :as str]))
(defn valid-room? [[encrypted _ checksum]]
(->> encrypted
frequencies
(sort-by (juxt (comp - val) key))
(take 5)
(map key)
(apply str)
(= checksum)))
(defn part-1 [rooms]
(transduce
(comp
(filter valid-room?)
(map second))
+
rooms))
(defn decrypt [[encrypted sector-id _]]
(->> (map
(fn [x]
(-> (int x)
(- 97)
(+ sector-id)
(mod 26)
(+ 97)
char))
encrypted)
(apply str)
(conj [sector-id])))
(defn part-2 [rooms]
(->> rooms
(map decrypt)
(aoc/find-first #(str/starts-with? (second %) "northpole"))
first))
(defn parse-line [line]
(let [[_ encrypted* id checksum] (re-find #"(\S+)-(\d+)\[(\w+)\]" line)
encrypted (str/replace encrypted* "-" "")
sector-id (parse-long id)]
[encrypted sector-id checksum]))
(defn solve [input]
(let [rooms (aoc/read-input input parse-line)]
((juxt part-1 part-2) rooms)))
(solve 4)