/
dayeleven.clj
74 lines (60 loc) · 1.87 KB
/
dayeleven.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
(ns adventofcode.dayeleven)
(def input-string (slurp "input/11.txt"))
(def sample-string "...#......\n.......#..\n#.........\n..........\n......#...\n.#........\n.........#\n..........\n.......#..\n#...#.....")
(defn pos [x y]
[x y]
)
(defn x [p]
(first p)
)
(defn y [p]
(second p)
)
(defn string->grid [string]
(reduce concat
(filter first
(map-indexed
(fn [y line]
(filter identity
(map-indexed
(fn [x c] (if (= c \#) (pos x y) nil))
line)))
(clojure.string/split-lines string))))
)
(defn empty-in-grid [g]
( let [mx (inc (reduce max (map first g))) my (inc (reduce max (map second g)))]
{
:columns (filter (fn [i] (empty? (filter #(= i (first %)) g))) (range mx))
:rows (filter (fn [i] (empty? (filter #(= i (second %)) g))) (range my))
})
)
(defn count-below [n coll]
(count (filter (partial >= n) coll))
)
(defn expand [g]
(let [{empty-cols :columns empty-rows :rows} (empty-in-grid g)]
(map (fn [[px py]]
(pos (+ px (count-below px empty-cols)) (+ py (count-below py empty-rows)))
) g)
)
)
(defn distance [p1 p2]
(+ (abs (- (x p1) (x p2))) (abs (- (y p1) (y p2))))
)
(defn sum-distances [g]
(reduce + (map-indexed (fn [i p] (reduce + (map #(distance p %) (drop (inc i) g)))) g))
)
(defn expand-large [g factor]
(let [{empty-cols :columns empty-rows :rows} (empty-in-grid g)]
(map (fn [[px py]]
(pos (+ px (* factor (count-below px empty-cols))) (+ py (* factor (count-below py empty-rows))))
) g)
)
)
(defn part-one-main [& args]
(sum-distances (expand (string->grid input-string)))
)
; part two
(defn -main [& args]
(println (sum-distances (expand-large (string->grid input-string) 999999)))
)