feat: added day 23 part 1 and 2
All checks were successful
Build and run challenges / Challenge for day (1) (push) Successful in 5s
Build and run challenges / Challenge for day (10) (push) Successful in 3s
Build and run challenges / Challenge for day (11) (push) Successful in 3s
Build and run challenges / Challenge for day (12) (push) Successful in 3s
Build and run challenges / Challenge for day (13) (push) Successful in 3s
Build and run challenges / Challenge for day (14) (push) Successful in 5s
Build and run challenges / Challenge for day (15) (push) Successful in 3s
Build and run challenges / Challenge for day (16) (push) Successful in 4s
Build and run challenges / Challenge for day (17) (push) Successful in 3s
Build and run challenges / Challenge for day (18) (push) Successful in 3s
Build and run challenges / Challenge for day (19) (push) Successful in 3s
Build and run challenges / Challenge for day (2) (push) Successful in 4s
Build and run challenges / Challenge for day (20) (push) Successful in 4s
Build and run challenges / Challenge for day (21) (push) Successful in 4s
Build and run challenges / Challenge for day (22) (push) Successful in 5s
Build and run challenges / Challenge for day (23) (push) Successful in 4s
Build and run challenges / Challenge for day (4) (push) Successful in 5s
Build and run challenges / Challenge for day (5) (push) Successful in 5s
Build and run challenges / Challenge for day (3) (push) Successful in 11s
Build and run challenges / Challenge for day (6) (push) Successful in 6s
Build and run challenges / Challenge for day (7) (push) Successful in 5s
Build and run challenges / Challenge for day (8) (push) Successful in 5s
Build and run challenges / Challenge for day (9) (push) Successful in 3s

Signed-off-by: Louis Vallat <contact@louis-vallat.dev>
This commit is contained in:
Louis Vallat 2025-01-21 22:51:17 +01:00
parent a2046b9692
commit 06265c2324
Signed by: louis
SSH Key Fingerprint: SHA256:0iPwDU/PZtEy/K13Oar4TzmcunmI9H5U9IsOR3jyT/Q
3 changed files with 101 additions and 1 deletions

View File

@ -11,7 +11,7 @@ jobs:
name: Challenge for day
strategy:
matrix:
day_number: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
day_number: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
runs-on: rust-bookworm
steps:
- name: Check out repository code

6
day23/Cargo.toml Normal file
View File

@ -0,0 +1,6 @@
[package]
name = "day23"
version = "0.1.0"
edition = "2021"
[dependencies]

94
day23/src/main.rs Normal file
View File

@ -0,0 +1,94 @@
use std::collections::{HashMap, HashSet};
use std::{env, fs};
fn read_input(path: &str) -> String {
fs::read_to_string(path).expect("Cannot read file.")
}
fn parse_input(input: &str) -> HashMap<&str, HashSet<&str>> {
let mut network = HashMap::new();
for connection in input.lines().filter(|l| !l.is_empty()) {
let pair = connection.split_once('-').unwrap();
network
.entry(pair.0)
.or_insert(HashSet::new())
.insert(pair.1);
network
.entry(pair.1)
.or_insert(HashSet::new())
.insert(pair.0);
}
network
}
fn part_1(network: &HashMap<&str, HashSet<&str>>) -> usize {
let mut triplets = HashSet::new();
let mut seen = HashSet::new();
for a in network.keys() {
for b in network.keys() {
let connections = network.get(a).unwrap();
if a == b
|| !connections.contains(b)
|| !(a.starts_with("t") || b.starts_with("t"))
|| seen.contains(&(a, b))
|| seen.contains(&(b, a))
{
continue;
}
seen.insert((a, b));
for common in connections.intersection(network.get(b).unwrap()) {
let mut triplet = vec![a, b, common];
triplet.sort();
triplets.insert(triplet);
}
}
}
triplets.len()
}
fn bron_kerbosch<'a>(
r: HashSet<&'a str>,
mut p: HashSet<&'a str>,
network: &HashMap<&'a str, HashSet<&'a str>>,
) -> Vec<&'a str> {
if p.is_empty() {
return r.iter().copied().collect();
}
let mut clique = Vec::new();
for computer in p.clone() {
let connections = network.get(computer).unwrap();
let res = bron_kerbosch(
r.union(&HashSet::from([computer])).copied().collect(),
p.intersection(connections).copied().collect(),
network,
);
if res.len() > clique.len() {
clique = res;
}
p.remove(computer);
}
clique
}
fn part_2(network: &HashMap<&str, HashSet<&str>>) -> String {
let mut best_clique =
bron_kerbosch(HashSet::new(), network.keys().copied().collect(), &network);
best_clique.sort();
best_clique.join(",")
}
fn main() {
let args: Vec<String> = env::args().collect();
for arg in args.iter().skip(1) {
let input = read_input(&arg);
let network = parse_input(&input);
println!("[{}]", &arg);
println!("\t[Part 1] => Answer is '{}'.", part_1(&network));
println!("\t[Part 2] => Answer is '{}'.", part_2(&network));
}
}