|
| 1 | +(ns day11 |
| 2 | + (:require [clojure.string :as str]) |
| 3 | + (:require [clojure.core.reducers :as reducers])) |
| 4 | + |
| 5 | +(defn calculate-sum-of-distance [lines scale] |
| 6 | + (let [rows (into [] (map (fn [x] (str/split x #"")) lines)) |
| 7 | + expanding-rows (map first (filter (fn [[idx va]] (every? #(= "." %1) va)) |
| 8 | + (map-indexed list rows))) |
| 9 | + cols (apply mapv vector rows) |
| 10 | + expanding-cols (map first (filter (fn [[idx va]] (every? #(= "." %1) va)) |
| 11 | + (map-indexed list cols))) |
| 12 | + galaxies (filter (fn [[r c]] (= (get (get rows r) c) "#")) |
| 13 | + (apply concat (map |
| 14 | + (fn [r] (map #(list r %1) (range (count cols)))) |
| 15 | + (range (count rows))))) |
| 16 | + count-in-range (fn [vals lo hi] |
| 17 | + (count (filter #(and (< lo %1) (< %1 hi)) vals))) |
| 18 | + calc-distance (fn [[[r1 c1] [r2 c2]]] |
| 19 | + (let [calc-axis-parallel-distance |
| 20 | + (fn [a b expaning-elems] |
| 21 | + (let [num-of-expanding-elems |
| 22 | + (count-in-range expaning-elems (min a b) (max a b)) |
| 23 | + raw-distance (abs (- a b))] |
| 24 | + (+ (- raw-distance num-of-expanding-elems) |
| 25 | + (* scale num-of-expanding-elems))))] |
| 26 | + (+ (calc-axis-parallel-distance r1 r2 expanding-rows) |
| 27 | + (calc-axis-parallel-distance c1 c2 expanding-cols)))) |
| 28 | + cross-product (apply concat (for [g1 galaxies] |
| 29 | + (for [g2 galaxies |
| 30 | + :when (not (= g1 g2))] |
| 31 | + [g1 g2])))] |
| 32 | + |
| 33 | + (println (/ (reducers/fold + (map calc-distance cross-product)) 2)))) |
| 34 | + |
| 35 | +(defn -main [filename] |
| 36 | + (with-open [file (clojure.java.io/reader filename)] |
| 37 | + (let [lines (remove str/blank? (line-seq file))] |
| 38 | + (calculate-sum-of-distance lines 2) |
| 39 | + (calculate-sum-of-distance lines 1000000)))) |
0 commit comments