Compare commits

..

3 Commits

Author SHA1 Message Date
237ca36381
day23: clippies
Some checks failed
test / AoC 2024 (push) Failing after 4m17s
2024-12-23 00:57:52 -08:00
d3bade1bdd
day23: part 2 - bron kerbosch solution 2024-12-23 00:54:23 -08:00
50a197856a
day23: part 2 working (i think) but too slow 2024-12-23 00:23:36 -08:00
3 changed files with 50 additions and 7 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.5 KiB

After

Width:  |  Height:  |  Size: 9.1 KiB

View File

@ -1,6 +1,6 @@
<!-- AOC TILES BEGIN --> <!-- AOC TILES BEGIN -->
<h1 align="center"> <h1 align="center">
2024 - 44 ⭐ - Rust 2024 - 45 ⭐ - Rust
</h1> </h1>
<a href="src/day1.rs"> <a href="src/day1.rs">
<img src=".aoc_tiles/tiles/2024/01.png" width="161px"> <img src=".aoc_tiles/tiles/2024/01.png" width="161px">

View File

@ -1,8 +1,7 @@
use std::fmt::Debug;
use aoc_runner_derive::aoc; use aoc_runner_derive::aoc;
use itertools::Itertools; use itertools::Itertools;
use rustc_hash::{FxHashMap, FxHashSet}; use rustc_hash::{FxHashMap, FxHashSet};
use std::fmt::{Debug, Display};
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)] #[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
struct Node([char; 2]); struct Node([char; 2]);
@ -13,6 +12,12 @@ impl Debug for Node {
} }
} }
impl Display for Node {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!("{}", self.0.iter().join("")))
}
}
impl From<[char; 2]> for Node { impl From<[char; 2]> for Node {
fn from(value: [char; 2]) -> Self { fn from(value: [char; 2]) -> Self {
Node(value) Node(value)
@ -51,6 +56,38 @@ impl Network {
} }
sets sets
} }
fn bron_kerbosch(
&self,
r: FxHashSet<Node>,
mut p: FxHashSet<Node>,
mut x: FxHashSet<Node>,
) -> Vec<FxHashSet<Node>> {
let mut results = Vec::new();
if p.is_empty() && x.is_empty() {
return vec![r];
}
let p_iter = p.clone(); // so we can modify p
for node in &p_iter {
let mut new_r = r.clone();
new_r.insert(*node);
let neighbours = FxHashSet::from_iter(self.edges.get(node).unwrap().iter().copied());
let new_p = FxHashSet::from_iter(p.intersection(&neighbours).copied());
let new_x = FxHashSet::from_iter(x.intersection(&neighbours).copied());
results.extend(self.bron_kerbosch(new_r, new_p, new_x).into_iter());
p.remove(node);
x.insert(*node);
}
results
}
fn maximal_subgraphs(&self) -> Vec<FxHashSet<Node>> {
self.bron_kerbosch(
FxHashSet::default(),
FxHashSet::from_iter(self.nodes.iter().copied()),
FxHashSet::default(),
)
}
} }
impl From<&str> for Network { impl From<&str> for Network {
@ -83,17 +120,23 @@ fn parse(input: &str) -> Network {
#[aoc(day23, part1)] #[aoc(day23, part1)]
pub fn part1(input: &str) -> i64 { pub fn part1(input: &str) -> i64 {
let network = parse(input); let network = parse(input);
println!("edges: {:?}", network.edges); // println!("edges: {:?}", network.edges);
let sets = network.groups_3(); let sets = network.groups_3();
let t_count = sets.iter().filter(|set| set.iter().any(|s| s.0[0] == 't')).count(); let t_count = sets.iter().filter(|set| set.iter().any(|s| s.0[0] == 't')).count();
println!("groups: {:?}", sets); // println!("groups: {:?}", sets);
t_count as i64 t_count as i64
} }
#[aoc(day23, part2)] #[aoc(day23, part2)]
pub fn part2(input: &str) -> String { pub fn part2(input: &str) -> String {
todo!() let network = parse(input);
let best_sets = network.maximal_subgraphs();
let largest_set = best_sets.iter().max_by(|a, b| a.len().cmp(&b.len())).unwrap();
let mut largest = largest_set.iter().collect_vec();
largest.sort();
println!("best: {:?}", largest);
largest.iter().join(",")
} }
#[cfg(test)] #[cfg(test)]