-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday24.clj
47 lines (38 loc) · 1.21 KB
/
day24.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
(ns advent-2020-clojure.day24
(:require [clojure.string :as str]))
(def directions {:e [2 0] :ne [1 1] :se [1 -1] :w [-2 0] :nw [-1 1] :sw [-1 -1]})
(defn parse-path [input]
(->> input (re-seq #"[n|s]?[e|w]") (map keyword)))
(defn tile-at [path]
(reduce #(mapv + %1 %2)
[0 0]
(map #(directions %) path)))
(defn initial-black-tiles [input]
(->> (str/split-lines input)
(map (partial (comp tile-at parse-path)))
frequencies
(keep (fn [[tile freq]] (when (odd? freq) tile)))
(into #{})))
(defn part1 [input]
(-> input initial-black-tiles count))
(defn adjacencies [tile]
(->> (vals directions)
(map #(mapv + tile %))))
(defn next-side-black? [tile black-tiles]
(let [black-adjacent (->> (adjacencies tile)
(filter #(black-tiles %))
count)]
(if (black-tiles tile)
(#{1 2} black-adjacent)
(= 2 black-adjacent))))
(defn next-turn [tile-set]
(->> tile-set
(mapcat adjacencies)
(into tile-set)
(filter #(next-side-black? % tile-set))
set))
(defn part2 [input]
(->> (iterate next-turn (initial-black-tiles input))
(drop 100)
(map count)
first))