Skip to content

Instantly share code, notes, and snippets.

@bozdoz
Created December 30, 2024 15:36
Show Gist options
  • Save bozdoz/81988ac83cdb416c283f03ae98331014 to your computer and use it in GitHub Desktop.
Save bozdoz/81988ac83cdb416c283f03ae98331014 to your computer and use it in GitHub Desktop.
aoc 23
#![allow(unused)]
use std::collections::{ HashMap, HashSet };
fn main() {
let networks = "kh-tc
qp-kh
de-cg
ka-co
yn-aq
qp-ub
cg-tb
vc-aq
tb-ka
wh-tc
yn-cg
kh-ub
ta-co
de-co
tc-td
tb-wq
wh-td
ta-ka
td-qp
aq-cg
wq-ub
ub-vc
de-ta
wq-aq
wq-vc
wh-yn
ka-de
kh-ta
co-tc
wh-qp
tb-vc
td-yn
";
let mut map: HashMap<&str, HashSet<&str>> = HashMap::new();
for line in networks.lines() {
let (a,b) = line.split_once("-").unwrap();
map.entry(a).and_modify(|x| {
x.insert(b);
}).or_insert_with(|| {
HashSet::from([b])
});
map.entry(b).and_modify(|x| {
x.insert(a);
}).or_insert_with(|| {
HashSet::from([a])
});
}
let mut trios = HashSet::new();
for (a, a_set) in map.iter() {
// println!("{a} {a_set:?}");
for b in a_set.iter() {
for c in map[b].iter() {
if a_set.contains(c) {
let mut nets = [a,b,c];
nets.sort();
trios.insert(nets);
}
}
}
}
let count = trios.iter().filter(|x| {
for a in x.iter() {
if a.starts_with('t') {
return true;
}
}
false
}).count();
println!("{count} with 't'");
let mut biggest = HashMap::new();
for (a, a_set) in map.iter() {
let mut visited = HashSet::from([a]);
let mut cur = HashSet::from([a]);
let mut queue = a_set.into_iter().collect::<Vec<_>>();
while let Some(v) = queue.pop() {
if visited.contains(&v) {
continue;
}
if map[v].contains(a) {
// keep going
cur.insert(v);
for b in map[v].iter() {
queue.push(b);
}
}
visited.insert(v);
}
biggest.insert(a, cur);
}
let mut counts = HashMap::new();
// check for intersections in all queues
for (a, set) in biggest.iter() {
for b in set {
if a == b {
continue;
}
let other = &biggest[b];
let mut intersection = set.intersection(other).collect::<Vec<_>>();
intersection.sort();
counts.entry(intersection).and_modify(|x| {
*x += 1;
}).or_insert(1);
}
}
let mut biggest = (0, vec![]);
for (k,v) in counts {
if v > biggest.0 {
biggest = (v, k);
}
}
println!("{}", biggest.1.into_iter().map(|x| x.to_string()).collect::<Vec<_>>().join(","))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment