-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday23.clj
35 lines (29 loc) · 1.2 KB
/
day23.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
(ns advent-2024-clojure.day23
(:require [clojure.string :as str]
[abyala.advent-utils-clojure.core :as c]))
(defn parse-connections [input]
(reduce (fn [acc [a b]] (-> acc
(update a (fnil conj (sorted-set)) b)
(update b (fnil conj (sorted-set)) a)))
{}
(partition 2 (re-seq #"\w+" input))))
(defn networks-with [connections n computer]
(map #(set (conj % computer)) (c/unique-combinations n (connections computer))))
(defn shared-networks [connections n]
(->> (keys connections)
(mapcat (partial networks-with connections (dec n)))
frequencies
(filter #(= n (second %)))
(map first)))
(defn part1 [input]
(c/count-when (fn [network] (some #(str/starts-with? % "t") network))
(shared-networks (parse-connections input) 3)))
(defn largest-network
([connections] (largest-network connections (-> connections vals first count inc)))
([connections n]
(when (> n 1)
(if-some [network (first (shared-networks connections n))]
(str/join "," (sort network))
(recur connections (dec n))))))
(defn part2 [input]
(largest-network (parse-connections input)))