-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday08.clj
45 lines (38 loc) · 1.24 KB
/
day08.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
(ns day08
(:require aoc))
(defn visible-from-outside? [^long height directions]
(reduce
(fn [acc dir]
(if (every? #(< ^long % height) dir)
(reduced true)
acc))
false
directions))
(defn viewing-distance [^long height direction]
(reduce
(fn [^long acc ^long h]
(if (< h height)
(inc acc)
(reduced (min (count direction) (inc acc)))))
0
direction))
(defn go-through-forrest [height-map]
(let [hor height-map
vert (aoc/transpose height-map)]
(for [[^long y row] (map-indexed vector hor)
[^long x col] (map-indexed vector vert)
:let [dirs [(rseq (subvec row 0 x)) ; left
(subvec row (inc x)) ; right
(rseq (subvec col 0 y)) ; up
(subvec col (inc y))] ; down
height (row x)]]
{:is-visible? (visible-from-outside? height dirs)
:scenic-score (transduce (map #(viewing-distance height %)) * dirs)})))
(defn solve
([] (solve (aoc/read-file 8)))
([input]
(let [height-map (aoc/parse-input input :digits)
results (go-through-forrest height-map)]
[(->> results (aoc/count-if :is-visible?))
(->> results (map :scenic-score) (reduce max))])))
(solve)