-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday13.clj
53 lines (44 loc) · 1.16 KB
/
day13.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
(ns day13
(:require aoc))
(defn compair [l r]
(cond
(and (number? l) (number? r)) (- l r)
(and (number? l) (sequential? r)) (compair [l] r)
(and (sequential? l) (number? r)) (compair l [r])
:else (loop [[hd & tl] (map compair l r)]
(case hd
nil (- (count l) (count r))
0 (recur tl)
hd))))
(defn part-1 [packets]
(->> packets
(map-indexed (fn [idx [l r]]
(if (neg? (compair l r))
(inc idx)
0)))
(reduce +)))
(defn divider-index [packets target]
(->> packets
(remove #(pos? (compair % target)))
count))
(defn part-2 [packets]
(let [two [[2]]
six [[6]]]
(->> [two six]
(into packets)
(reduce concat)
(#(* (divider-index % two)
(divider-index % six))))))
(defn parse-input [input]
(->> input
aoc/parse-input
(remove empty?)
(mapv read-string)
(partition 2)))
(defn solve
([] (solve (aoc/read-file 13)))
([input]
(let [packets (parse-input input)]
[(part-1 packets)
(part-2 packets)])))
(solve)