-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday01_viz.clj
67 lines (54 loc) · 1.56 KB
/
day01_viz.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
(ns day01-viz
(:require aoc
day01
[quil.core :as q]
[quil.middleware :as m]))
(def window-size 700)
(def instructions (->> (aoc/read-input-line 1 :words)
(map day01/parse-instruction)))
(defn rand-color []
(take 3 (repeatedly #(+ 100 (rand-int 155)))))
(defn setup []
(q/frame-rate 60)
(q/smooth)
(q/background 15 15 33)
(q/stroke-weight 3)
{:cnt 0
:pos [0 0]
:color (rand-color)
:dir [0 -1]
:instr instructions})
(defn update-state [{:keys [cnt pos dir instr] :as state}]
(if (empty? instr)
(do
(q/delay-frame 2000)
(q/exit))
(let [new-pos (aoc/pt+ pos dir)
new-dir (day01/rotate dir (:rot (first instr)))]
(cond
(zero? cnt)
(-> state
(update :cnt inc)
(assoc :dir new-dir))
(<= cnt (:amt (first instr)))
(-> state
(update :cnt inc)
(assoc :pos new-pos))
:else
(-> state
(assoc :color (rand-color)
:cnt 0)
(update :instr rest))))))
(defn draw-state [{:keys [pos color]}]
(q/with-translation [(* 3/4 window-size) (* 9/10 window-size)]
(q/with-stroke color
(when-let [[x y] pos]
(q/point (* 3 x) (* 3 y))
#_(q/save-frame "/tmp/imgs/day01-####.jpg")))))
(q/sketch
:size [window-size window-size]
:setup #'setup
:update #'update-state
:draw #'draw-state
:middleware [m/fun-mode])
; convert -layers optimize -delay 2 /tmp/imgs/day01*.jpg imgs/day01.gif