-
Notifications
You must be signed in to change notification settings - Fork 0
/
day08.clj
48 lines (40 loc) · 1.08 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
46
47
48
(ns aoc.2022.day08
(:require
[aoc.grid :as grid]
[aoc.core :refer [read-input-lines map-all]]
[medley.core :as m]))
(defn visible? [g coord]
(->> grid/cardinals
(map #(grid/coords-in-dir g coord %))
(map-all #(get g %))
(map (fn [neighbors] (every? #(< % (get g coord)) neighbors)))
(some true?)))
(defn part1 [fname]
(let [g (grid/to-grid (read-input-lines fname) parse-long)]
(->> (keys g)
(map #(visible? g %))
(filter true?)
(count))))
(defn scenic-score [g coord]
(->> grid/cardinals
(map #(grid/coords-in-dir g coord %))
(map-all #(get g %))
(map-all #(< % (get g coord)))
(map #(m/take-upto false? %))
(map count)
(apply *)))
(defn part2 [fname]
(let [g (grid/to-grid (read-input-lines fname) parse-long)]
(->> (keys g)
(map #(scenic-score g %))
(apply max))))
(comment
(part1 "2022/day08-sample.txt")
;; => 21
(part1 "2022/day08.txt")
;; => 1807
(part2 "2022/day08-sample.txt")
;; => 8
(part2 "2022/day08.txt")
;; => 480000
)