This commit is contained in:
Aleh Suprunovich 2025-12-08 17:40:01 +03:00
parent 4723e304ab
commit 1006f6960c
2 changed files with 87 additions and 2 deletions

View File

@ -48,8 +48,6 @@
(recur (set (propagate beams))) (recur (set (propagate beams)))
(println "Beam split" @split "times"))) (println "Beam split" @split "times")))
(def memo (atom {}))
(def timelines (def timelines
(memoize (memoize
(fn [[l c]] (fn [[l c]]

87
src/day8.clj Normal file
View File

@ -0,0 +1,87 @@
(ns day8
(:require
[clojure.edn :as edn]
[clojure.math :as math]
[clojure.string :as string]
[helpers :refer [get-input]]))
(def example
"162,817,812
57,618,57
906,360,560
592,479,940
352,342,300
466,668,158
542,29,236
431,825,988
739,650,466
52,470,668
216,146,977
819,987,18
117,168,530
805,96,715
346,949,466
970,615,88
941,993,340
862,61,35
984,92,344
425,690,689")
;(def input (->> (string/split-lines example)
; (map #(string/split % #","))
; (map (partial map edn/read-string))))
(def input (->> (string/split-lines (get-input 8))
(map #(string/split % #","))
(map (partial map edn/read-string))))
(def d
(memoize
(fn [a b]
(->>
(map - a b)
(map #(* % %))
(reduce +)
math/sqrt))))
(def distances
(->>
(mapcat (fn [a] (mapcat (fn [b] [[a b] (d a b)]) input)) input)
(apply hash-map)
(remove (comp zero? val))
(sort-by val)))
(defn connect! [connections [a b]]
(let [ca (first (filter #(some % [a]) connections))
cb (first (filter #(some % [b]) connections))
c' (remove #(some % [a b]) connections)]
(cond
(and (empty? ca) (not-empty cb))
(conj c' (conj cb a))
(and (not-empty ca) (empty? cb))
(conj c' (conj ca b))
(and (not-empty ca) (not-empty cb))
(conj c' (into ca cb))
:else
(conj connections #{a b}))))
(loop [connections []
;; doubling number of connections to skip dealing with double distances betweer A -> B and B -> A
;;distances (take 20 distances)]
distances (take 2000 distances)]
(if (empty? distances)
(println "Result:"
(->> (sort-by count > connections)
(take 3)
(map count)
(reduce * 1)))
(recur (connect! connections (key (first distances))) (rest (rest distances)))))
(loop [connections []
distances distances]
(let [pair (key (first distances))
connections' (connect! connections pair)]
;;(if (= 20 (count (first connections')))
(if (= 1000 (count (first connections')))
(println "Pair:" pair, "multiplying X coordinates:" (apply * (map first pair)))
(recur connections' (rest (rest distances))))))