-
Notifications
You must be signed in to change notification settings - Fork 3
/
day09.clj
44 lines (32 loc) · 1.08 KB
/
day09.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
(ns day09
(:require aoc
[clojure.core.match :refer [match]]))
(def pattern #"([A-Z]*)\((\d+)x(\d+)\)")
(defn find-pattern [compressed]
(if-let [[patt pre* len* times*] (re-find pattern compressed)]
(let [pre-len (count pre*)
len (parse-long len*)
times (parse-long times*)
marker-len (count patt)]
[pre-len len times marker-len])
(count compressed)))
(defn decompress
([compressed bomb?]
(decompress compressed 0 bomb?))
([compressed len-so-far bomb?]
(match (find-pattern compressed)
[pre-len len times marker-len]
(let [remaining (drop marker-len compressed)
[took left] (map #(apply str %) (split-at len remaining))
decompressed-len (if bomb? (decompress took true) len)
seen (+ len-so-far
pre-len
(* times decompressed-len))]
(recur left seen bomb?))
n
(+ len-so-far n))))
(defn solve [input]
(let [compressed (aoc/read-input-line input)]
(for [bomb? [false true]]
(decompress compressed bomb?))))
(solve 9)