Skip to content

Commit

Permalink
2024_23
Browse files Browse the repository at this point in the history
  • Loading branch information
hsaikia committed Dec 23, 2024
1 parent 9159b7c commit 9c7dd27
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
98 changes: 98 additions & 0 deletions src/bin/2024_23/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use std::collections::HashMap;

use aoc::{
common::{self, HashMapVector},
io,
};
use itertools::Itertools;

fn solve<const PART: usize>(input: &str) -> String {
let mut conn = HashMap::new();
let mut sccs: Vec<Vec<&str>> = Vec::new();
for line in input.lines() {
let p = io::tokenize(line, "-");
conn.add_to_vector_hashmap(&p[0], p[1]);
conn.add_to_vector_hashmap(&p[1], p[0]);
sccs.push(p);
}

if PART == 1 {
let mut three_sets = Vec::new();
for k1 in conn.keys() {
if !k1.starts_with('t') {
continue;
}

for k2 in conn.keys() {
if k1 == k2 {
continue;
}
if !conn.contains(k1, k2) {
continue;
}

for k3 in conn.keys() {
if k2 == k3 || k1 == k3 {
continue;
}

if !conn.contains(k1, k3) || !conn.contains(k2, k3) {
continue;
}

let mut tmp = [k1, k2, k3];
tmp.sort();
three_sets.push(tmp);
}
}
}
three_sets.sort();
three_sets.dedup();
three_sets.len().to_string()
} else {
loop {
sccs.sort();
sccs.dedup();

let max_scc_size = sccs.iter().map(|s| s.len()).max().unwrap();
sccs.retain(|x| x.len() == max_scc_size);
//println!("Scc Max Size {} Number SCCs {}", max_scc_size, sccs.len());
if sccs.len() == 1 {
break;
}

for scc in sccs.iter_mut() {
for k in conn.keys() {
if !scc.contains(k) {
let all_conn = scc.iter().all(|x| conn.get(x).unwrap().contains(k));
if all_conn {
scc.push(k);
scc.sort();
}
}
}
}
}

sccs[0].iter().join(",")
}
}

fn main() {
let input = common::get_input();
println!("{input:?}");
common::timed(&input, solve::<1>, true);
common::timed(&input, solve::<2>, false);
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_samples() {
let sample_input = "kh-tc\nqp-kh\nde-cg\nka-co\nyn-aq\nqp-ub\ncg-tb\nvc-aq\ntb-ka\nwh-tc\nyn-cg\nkh-ub\nta-co\nde-co\ntc-td\ntb-wq\nwh-td\nta-ka\ntd-qp\naq-cg\nwq-ub\nub-vc\nde-ta\nwq-aq\nwq-vc\nwh-yn\nka-de\nkh-ta\nco-tc\nwh-qp\ntb-vc\ntd-yn";
assert_eq!(solve::<1>(sample_input), "7");
assert_eq!(solve::<2>(sample_input), "co,de,ka,ta");
}
}
13 changes: 13 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use std::{

pub trait HashMapVector<K: Copy + Hash + Eq + PartialEq, V> {
fn add_to_vector_hashmap(&mut self, key: &K, value: V);
fn contains(&self, key: &K, value: &V) -> bool
where
V: PartialEq;
}

impl<K: Copy + Hash + Eq + PartialEq, V, S: BuildHasher> HashMapVector<K, V>
Expand All @@ -23,6 +26,16 @@ impl<K: Copy + Hash + Eq + PartialEq, V, S: BuildHasher> HashMapVector<K, V>
}
}
}

fn contains(&self, key: &K, value: &V) -> bool
where
V: PartialEq,
{
if let Some(v) = self.get(key) {
return v.contains(value);
}
false
}
}

pub fn minmax<T: Ord + Copy>(x: &T, y: &T) -> (T, T) {
Expand Down

0 comments on commit 9c7dd27

Please sign in to comment.