-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10.clj
43 lines (38 loc) · 1.18 KB
/
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
42
43
(ns clojure-solutions.day10
(:require [clojure.string :as str])
(:use [clojure-aoc-util.util] :reload))
(defn- parse []
(->> (slurp "../inputs/day10.txt")
str/split-lines
(map (fn [ins]
(let [[_ a] (words ins)]
(if a [:add (read-string a)] :noop))))))
(defn- simulate [inp]
(letfn [(step [x i]
(if (= :noop i) [x] [x (+ x (second i))]))]
(apply concat
(reductions (fn [acc ins] (step (peek acc) ins))
[1]
inp))))
(defn- part-two [inp]
(letfn [(draw-pixel? [pos sprite]
(<= (dec sprite) (mod pos 40) (inc sprite)))]
(let [pixels (map (fn [row pos]
(if (draw-pixel? row pos) "█" " "))
(range 0 (count inp))
inp)]
(->> pixels
(partition 40)
(map str/join)
(cons "\n")
(str/join "\n")))))
(defn- part-one [inp]
(letfn [(calc [n] (* n (nth inp (dec n))))]
(reduce (fn [acc e] (+ acc (calc e)))
0
[20 60 100 140 180 220])))
(defn day10 [p]
((case p
:one part-one
:two part-two)
(simulate (parse))))