-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.clj
52 lines (46 loc) · 1.35 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
(ns day-04.core
(:gen-class))
(defn parse-room
[room-str]
(let [matches (re-matches #"([a-z\-]+)-([\d]+)\[([a-z]{5})\]" room-str)]
{:enc-name (nth matches 1)
:sector (Integer/parseUnsignedInt (nth matches 2))
:checksum (nth matches 3)}))
(defn verify-checksum
[room]
(let [room-name (:enc-name room)]
(->> (filter (partial not= \-) room-name)
(frequencies)
(map vec)
(sort #(if (= (second %1) (second %2))
(compare (first %1) (first %2))
(> (second %1) (second %2))))
(take 5)
(map first)
(= (seq (:checksum room))))))
(defn decode-character
[rot c]
(if (= \- c) \space
(char (+ (int \a)
(mod (+ (- (int c) (int \a)) rot)
26)))))
(defn decrypt-room
[room]
(assoc room :name (apply str (map (partial decode-character (:sector room)) (:enc-name room)))))
(defn -main
"I don't do a whole lot ... yet."
[& args]
(->> (slurp "input.txt")
(clojure.string/split-lines)
(map parse-room)
(filter verify-checksum)
(map :sector)
(reduce +)
(println))
(->> (slurp "input.txt")
(clojure.string/split-lines)
(map parse-room)
(filter verify-checksum)
(map decrypt-room)
(filter #(clojure.string/includes? % "object"))
(println)))