/
day_08.clj
48 lines (40 loc) · 1.05 KB
/
day_08.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
(ns advent-of-code-2015.day-08
(:gen-class))
(defn parse-escaped-chars
[s]
(-> (subs s 1 (dec (count s)))
(clojure.string/replace "\\\\" "\\")
(clojure.string/replace "\\\"" "\"")
(clojure.string/replace #"(\\x[0-9a-f][0-9a-f])" "X")))
(defn encode-string
[s]
(str \"
(-> s (clojure.string/replace "\\" "\\\\") (clojure.string/replace "\"" "\\\""))
\"))
(defn length-in-code
[s]
(count s))
(defn length-in-memory
[s]
(count (parse-escaped-chars s)))
(defn encoded-length
[s]
(count (encode-string s)))
(defn part-1
[input]
(reduce +
(map #(- (length-in-code %)
(length-in-memory %))
(clojure.string/split-lines input))))
(defn part-2
[input]
(reduce +
(map #(- (encoded-length %)
(length-in-code %))
(clojure.string/split-lines input))))
(defn day-08
[input-file]
(let [input (slurp input-file)]
(println "Day 8")
(println "Δchars: " (part-1 input))
(println "Δchars_encoded: " (part-2 input))))