-
Notifications
You must be signed in to change notification settings - Fork 0
/
day16.clj
48 lines (45 loc) · 1.75 KB
/
day16.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 clojure-solutions.day16
(:require [clojure.string :as str]
[clojure-aoc-util.util :as aoc]
[clojure-aoc-util.coords :as c]))
(defn- parse []
(aoc/transpose (str/split-lines (slurp "../inputs/day16.txt"))))
(defn- simulate [grid start]
(aoc/flood-fill
start
(fn [[beam dir] seen]
(filter #(and (aoc/not-in? seen %)
(aoc/mat-ix grid (first %)))
(case (aoc/mat-ix grid beam)
\. [[(c/move dir beam) dir]]
\/ [(case dir
:E [(c/above beam) :N]
:N [(c/right beam) :E]
:S [(c/left beam) :W]
:W [(c/below beam) :S])]
\\ [(case dir
:E [(c/below beam) :S]
:S [(c/right beam) :E]
:N [(c/left beam) :W]
:W [(c/above beam) :N])]
\- (if (contains? #{:N :S} dir)
[[(c/left beam) :W], [(c/right beam) :E]]
[[(c/move dir beam) dir]])
\| (if (contains? #{:E :W} dir)
[[(c/above beam) :N], [(c/below beam) :S]]
[[(c/move dir beam) dir]]))))))
(defn day16 [p]
(let [grid (parse)
xmax (count grid)
ymax (count (first grid))
starts (case p
:one [[[0 0] :E]]
:two (concat
(mapcat (fn [x] [[[x 0] :S] [[x (dec ymax)] :N]])
(range xmax))
(mapcat (fn [y] [[[0 y] :E] [[(dec xmax) y] :W]])
(range ymax))))]
(->> starts
(pmap #(into #{} (map first) (simulate grid %)))
(map count)
(apply max))))