-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday10.clj
More file actions
58 lines (50 loc) · 1.96 KB
/
day10.clj
File metadata and controls
58 lines (50 loc) · 1.96 KB
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
(ns day10
(:require [aoc-utils.core :as aoc]))
(defn find-start [sketch]
(first (for [[j row] (map-indexed vector sketch)
[i c] (map-indexed vector row)
:when (= \S c)]
[i j])))
(defn traverse [sketch start]
(loop [[x y :as curr] (aoc/pt+ start [0 1])
[px py] start
seen (transient #{})
verticals (transient {})]
(let [seen' (conj! seen curr)
dx (- x px)
dy (- y py)
vy (verticals y)]
(case ((sketch y) x)
\S {:pipes seen'
:verts (persistent! verticals)}
\J (recur (if (zero? dy) [x (dec y)] [(dec x) y])
curr seen' (assoc! verticals y (conj vy x)))
\L (recur (if (zero? dy) [x (dec y)] [(inc x) y])
curr seen' (assoc! verticals y (conj vy x)))
\7 (recur (if (zero? dy) [x (inc y)] [(dec x) y])
curr seen' verticals)
\F (recur (if (zero? dy) [x (inc y)] [(inc x) y])
curr seen' verticals)
\| (recur [(+ x dx) (+ y dy)]
curr seen' (assoc! verticals y (conj vy x)))
\- (recur [(+ x dx) (+ y dy)]
curr seen' verticals)))))
(defn enclosed [seen verticals h w]
(aoc/do-count [y (range h)
:let [row-verts (verticals y)]
:when row-verts
:let [min-vert (reduce min row-verts)
max-vert (reduce max row-verts)]
x (range w)
:when (and (< min-vert x max-vert)
(not (seen [x y]))
(odd? (aoc/count-if #(< % x) row-verts)))]))
(defn solve [input]
(let [sketch (aoc/parse-lines input :chars)
start (find-start sketch)
h (count sketch)
w (count (first sketch))
{:keys [pipes verts]} (traverse sketch start)]
[(/ (count pipes) 2)
(enclosed pipes verts h w)]))
(solve (aoc/read-input 10))