-
Notifications
You must be signed in to change notification settings - Fork 0
/
day10.clj
76 lines (63 loc) · 2.06 KB
/
day10.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
(ns day10.clj
(:require [clojure.string :as str]))
;; input format: "position=<px, py> velocity=< vx, vy>
(defn parse-vector
[s]
(->> (str/split s #",")
(map #(Integer/parseInt (str/trim %)))
(zipmap [:x :y])))
(defn parse-point
[s]
(let [[_ position _ velocity] (str/split s #"[<>]")]
{:position (parse-vector position)
:velocity (parse-vector velocity)}))
(defn next-point
[{:keys [velocity] :as point}]
(update point :position #(merge-with + % velocity)))
(defn bounding-box
[points]
(let [positions (map :position points)]
{:origin {:x (apply min (map :x positions))
:y (apply min (map :y positions))}
:corner {:x (apply max (map :x positions))
:y (apply max (map :y positions))}}))
(defn area
[{:keys [origin corner]}]
(* (- (:y corner) (:y origin))
(- (:x corner) (:x origin))))
(defn first-min-key
[k coll]
(loop [[y & more] (rest coll)
x (first coll)
kx (k (first coll))]
(let [ky (k y)]
(if (< kx ky)
x
(if more (recur more y ky) y)))))
(defn display-configuration
[configuration]
(let [{:keys [origin corner]} (bounding-box configuration)]
(println "")
(println (str/join "\n"
(for [y (range (:y origin) (inc (:y corner)))]
(apply str
(for [x (range (:x origin) (inc (:x corner)))]
(if (some #(and (= x (get-in % [:position :x]))
(= y (get-in % [:position :y])))
configuration)
\X
\.))))))))
(def configurations
(->> (slurp "day10.txt")
str/split-lines
(map parse-point)
(iterate #(map next-point %))))
;; Part One
(->> configurations
(first-min-key #(area (bounding-box %)))
display-configuration)
;; Part Two
(->> configurations
(map-indexed vector)
(first-min-key #(area (bounding-box (second %))))
first)