Compare commits
3 Commits
d3ce12693b
...
237ca36381
Author | SHA1 | Date | |
---|---|---|---|
237ca36381 | |||
d3bade1bdd | |||
50a197856a |
Binary file not shown.
Before Width: | Height: | Size: 7.5 KiB After Width: | Height: | Size: 9.1 KiB |
@ -1,6 +1,6 @@
|
||||
<!-- AOC TILES BEGIN -->
|
||||
<h1 align="center">
|
||||
2024 - 44 ⭐ - Rust
|
||||
2024 - 45 ⭐ - Rust
|
||||
</h1>
|
||||
<a href="src/day1.rs">
|
||||
<img src=".aoc_tiles/tiles/2024/01.png" width="161px">
|
||||
|
55
src/day23.rs
55
src/day23.rs
@ -1,8 +1,7 @@
|
||||
use std::fmt::Debug;
|
||||
|
||||
use aoc_runner_derive::aoc;
|
||||
use itertools::Itertools;
|
||||
use rustc_hash::{FxHashMap, FxHashSet};
|
||||
use std::fmt::{Debug, Display};
|
||||
|
||||
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
|
||||
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 {
|
||||
fn from(value: [char; 2]) -> Self {
|
||||
Node(value)
|
||||
@ -51,6 +56,38 @@ impl Network {
|
||||
}
|
||||
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 {
|
||||
@ -83,17 +120,23 @@ fn parse(input: &str) -> Network {
|
||||
#[aoc(day23, part1)]
|
||||
pub fn part1(input: &str) -> i64 {
|
||||
let network = parse(input);
|
||||
println!("edges: {:?}", network.edges);
|
||||
// println!("edges: {:?}", network.edges);
|
||||
let sets = network.groups_3();
|
||||
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
|
||||
}
|
||||
|
||||
#[aoc(day23, part2)]
|
||||
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)]
|
||||
|
Loading…
x
Reference in New Issue
Block a user