-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
1,329 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.