-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10.clj
41 lines (34 loc) · 894 Bytes
/
day10.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
(ns day10
(:require aoc
[clojure.string :as str]))
(defn read-instructions [lines]
(reduce
(fn [acc line]
(let [[instr amt] (str/split line #" ")]
(if (= instr "addx")
(conj acc 0 (parse-long amt))
(conj acc 0))))
[]
lines))
(defn part-1 [x-positions]
(->> (range 20 221 40)
(map #(* % (nth x-positions (dec %))))
(reduce +)))
(defn part-2 [x-positions]
(->> x-positions
(map-indexed #(if (<= (dec %2) (mod %1 40) (inc %2))
"██"
" "))
(partition 40)
(map str/join)))
(defn solve
([] (solve (aoc/read-file 10)))
([input]
(let [x-positions
(->> input
aoc/parse-input
read-instructions
(reductions + 1))] ; x starts at 1
[(part-1 x-positions)
(part-2 x-positions)])))
(solve)