Skip to content

Commit

Permalink
day25.clj
Browse files Browse the repository at this point in the history
  • Loading branch information
narimiran committed Sep 10, 2024
1 parent 15dc474 commit 6efdfa4
Show file tree
Hide file tree
Showing 5 changed files with 1,329 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Day 00: Helper file | [aoc.clj](clojure/aoc.clj) |
[Day 22](http://adventofcode.com/2023/day/22) | [day22.clj](clojure/day22.clj) | Jenga!
[Day 23](http://adventofcode.com/2023/day/23) | [day23.clj](clojure/day23.clj) | Compressing the graph for part 2.
[Day 24](http://adventofcode.com/2023/day/24) | [day24.clj](clojure/day24.clj) | Don't ask me how this works.
<!-- [Day 25](http://adventofcode.com/2023/day/25) | [day25.clj](clojure/day25.clj) | -->
[Day 25](http://adventofcode.com/2023/day/25) | [day25.clj](clojure/day25.clj) | Monte Carlo to find "hot" edges.


&nbsp;
Expand Down
61 changes: 61 additions & 0 deletions clojure/day25.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
(ns day25
(:require aoc))


(defn build-graph [components]
(let [adjacencies (atom {})]
(doseq [[k & vs] components
v vs]
(swap! adjacencies update k conj v)
(swap! adjacencies update v conj k))
@adjacencies))


(defn add-edges [edge-counts vs]
(reduce (fn [edge-counts [a b]]
(update edge-counts (sort [a b]) (fnil inc 0)))
edge-counts
(partition 2 1 vs)))

(defn traverse [graph start end edge-counts]
(loop [queue (conj aoc/empty-queue [start []])
seen #{}]
(if-let [[curr prevs] (peek queue)]
(cond
(= curr end) (add-edges edge-counts (conj prevs curr))
(seen curr) (recur (pop queue) seen)
:else (recur (into (pop queue) (map (fn [x] [x (conj prevs curr)])
(graph curr)))
(conj seen curr)))
(count seen))))

(defn find-most-frequent-edges [graph]
(reduce (fn [edge-counts [start end]]
(traverse graph start end edge-counts))
{}
(repeatedly 200 #(shuffle (keys graph)))))

(defn remove-most-frequent [edge-counts graph]
(let [most-frequent (keys (take 3 (sort-by val > edge-counts)))]
(reduce (fn [graph [a b]]
(-> graph
(update a (fn [nbs] (remove #{b} nbs)))
(update b (fn [nbs] (remove #{a} nbs)))))
graph
most-frequent)))

(defn find-groups [graph]
(let [edge-counts (find-most-frequent-edges graph)
graph' (remove-most-frequent edge-counts graph)
total-nodes (count (keys graph))
island (traverse graph' (key (first graph)) nil nil)]
(* (- total-nodes island) island)))


(defn solve [input-file]
(let [components (aoc/parse-input input-file :words {:word-sep #": | "})
graph (build-graph components)]
(find-groups graph)))


(solve (aoc/read-file 25))
3 changes: 2 additions & 1 deletion clojure/tests/solutions_tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
day06 day07 day08 day09 day10
day11 day12 day13 day14 day15
day16 day17 day18 day19 day20
day21 day22 day23 day24 ;day25
day21 day22 day23 day24 day25
[clojure.test :refer [deftest is run-tests successful?]]))


Expand Down Expand Up @@ -48,6 +48,7 @@
(check-day 22 [5 7] [418 70702])
(check-day 23 nil [2394 6554])
(check-day 24 nil [13892 843888100572888])
(check-day 25 54 603368)


(let [summary (run-tests)]
Expand Down
Loading

0 comments on commit 6efdfa4

Please sign in to comment.