add day 5
parent
136b7866f0
commit
4f63f785fa
@ -0,0 +1,2 @@
|
||||
.*.sw*
|
||||
**/in
|
@ -0,0 +1,89 @@
|
||||
(require '[clojure.string :as str])
|
||||
|
||||
(defn read-coords []
|
||||
(let [line (read-line)]
|
||||
(when (not (empty? line))
|
||||
(mapv
|
||||
#(Integer/parseInt %)
|
||||
(str/split
|
||||
line
|
||||
#"[^\d]+"
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
(defn mark-coord [cmap x y]
|
||||
(vec
|
||||
(for [c (range 0 (count cmap))]
|
||||
(if (= c y)
|
||||
(vec
|
||||
(for [r (range 0 (count cmap))]
|
||||
(if (= r x)
|
||||
(inc (get (get cmap c) r))
|
||||
(get (get cmap c) r)
|
||||
)
|
||||
)
|
||||
)
|
||||
(get cmap c)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
(defn mark-coords [cmap x1 y1 x2 y2]
|
||||
(cond
|
||||
(= y1 y2)
|
||||
(loop [cm cmap x (range (min x1 x2) (inc (max x1 x2)))]
|
||||
(if (empty? x)
|
||||
cm
|
||||
(recur
|
||||
(mark-coord cm (first x) y1)
|
||||
(rest x)
|
||||
)
|
||||
)
|
||||
)
|
||||
(= x1 x2)
|
||||
(loop [cm cmap y (range (min y1 y2) (inc (max y1 y2)))]
|
||||
(if (empty? y)
|
||||
cm
|
||||
(recur
|
||||
(mark-coord cm x1 (first y))
|
||||
(rest y)
|
||||
)
|
||||
)
|
||||
)
|
||||
:else
|
||||
cmap
|
||||
)
|
||||
)
|
||||
|
||||
(defn empty-map []
|
||||
(vec
|
||||
(repeat 1000
|
||||
(vec (repeat 1000 0))
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
(def finished-map
|
||||
(loop [cmap (empty-map) coord (read-coords)]
|
||||
(if (empty? coord)
|
||||
cmap
|
||||
(recur
|
||||
(apply (partial mark-coords cmap) coord)
|
||||
(read-coords)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
(->> finished-map
|
||||
(flatten)
|
||||
(map dec)
|
||||
(filter pos?)
|
||||
(count)
|
||||
(println)
|
||||
)
|
||||
|
@ -0,0 +1,102 @@
|
||||
(require '[clojure.string :as str])
|
||||
|
||||
(defn read-coords []
|
||||
(let [line (read-line)]
|
||||
(when (not (empty? line))
|
||||
(mapv
|
||||
#(Integer/parseInt %)
|
||||
(str/split
|
||||
line
|
||||
#"[^\d]+"
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
(defn mark-coord [cmap x y]
|
||||
(vec
|
||||
(for [c (range 0 (count cmap))]
|
||||
(if (= c y)
|
||||
(vec
|
||||
(for [r (range 0 (count cmap))]
|
||||
(if (= r x)
|
||||
(inc (get (get cmap c) r))
|
||||
(get (get cmap c) r)
|
||||
)
|
||||
)
|
||||
)
|
||||
(get cmap c)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
(defn mark-coords [cmap x1 y1 x2 y2]
|
||||
(cond
|
||||
(= y1 y2)
|
||||
(loop [cm cmap x (range (min x1 x2) (inc (max x1 x2)))]
|
||||
(if (empty? x)
|
||||
cm
|
||||
(recur
|
||||
(mark-coord cm (first x) y1)
|
||||
(rest x)
|
||||
)
|
||||
)
|
||||
)
|
||||
(= x1 x2)
|
||||
(loop [cm cmap y (range (min y1 y2) (inc (max y1 y2)))]
|
||||
(if (empty? y)
|
||||
cm
|
||||
(recur
|
||||
(mark-coord cm x1 (first y))
|
||||
(rest y)
|
||||
)
|
||||
)
|
||||
)
|
||||
:else
|
||||
(let [ic (if (< x1 x2) [x1 y1] [x2 y2])
|
||||
ec (if (> x1 x2) [x1 y1] [x2 y2])
|
||||
dy (if (> (second ec) (second ic)) 1 -1)
|
||||
]
|
||||
(loop [cm cmap c ic]
|
||||
(if (> (first c) (first ec))
|
||||
cm
|
||||
(recur
|
||||
(apply (partial mark-coord cm) c)
|
||||
[(inc (first c)) (+ dy (second c))]
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
(defn empty-map []
|
||||
(vec
|
||||
(repeat 1000
|
||||
(vec (repeat 1000 0))
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
(def finished-map
|
||||
(loop [cmap (empty-map) coord (read-coords)]
|
||||
(if (empty? coord)
|
||||
cmap
|
||||
(recur
|
||||
(apply (partial mark-coords cmap) coord)
|
||||
(read-coords)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
(->> finished-map
|
||||
(flatten)
|
||||
(map dec)
|
||||
(filter pos?)
|
||||
(count)
|
||||
(println)
|
||||
)
|
||||
|
Loading…
Reference in New Issue