Skip to content

Commit

Permalink
Day 23 solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
Cadiac committed Dec 23, 2024
1 parent 688652f commit 96d7fe8
Show file tree
Hide file tree
Showing 5 changed files with 3,549 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,4 @@ This should start the server at `localhost:8080`.
❄️ [Day 20](aoc-solver/src/y2024/day20.rs)
❄️ [Day 21](aoc-solver/src/y2024/day21.rs)
❄️ [Day 22](aoc-solver/src/y2024/day22.rs)
❄️ [Day 23](aoc-solver/src/y2024/day23.rs)
163 changes: 163 additions & 0 deletions aoc-solver/src/y2024/day23.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
use std::collections::{HashMap, HashSet};

use crate::solution::{AocError, Solution};

fn parse(input: &str) -> Result<HashMap<&str, HashSet<&str>>, AocError> {
let mut network: HashMap<&str, HashSet<&str>> = HashMap::new();

for row in input.lines() {
let (first, second) = row
.split_once("-")
.ok_or_else(|| AocError::parse(row, "Invalid network node"))?;

network.entry(first).or_default().insert(second);
network.entry(second).or_default().insert(first);
}

Ok(network)
}

pub struct Day23;
impl Solution for Day23 {
type A = u32;
type B = String;

fn default_input(&self) -> &'static str {
include_str!("../../../inputs/2024/day23.txt")
}

fn part_1(&self, input: &str) -> Result<u32, AocError> {
let network = parse(input)?;

let mut networks_of_three: HashSet<Vec<&str>> = HashSet::new();

for (first, edges) in network.iter() {
if first.starts_with("t") {
for second in edges {
for third in network[second].intersection(edges) {
let mut group = vec![*first, *second, *third];
group.sort();
networks_of_three.insert(group);
}
}
}
}

Ok(networks_of_three.len() as u32)
}

fn part_2(&self, input: &str) -> Result<String, AocError> {
let network = parse(input)?;

let mut largest = network
.iter()
.map(|(current, edges)| {
let mut interconnected = vec![*current];

for edge in edges {
if interconnected
.iter()
.all(|node| network[edge].contains(node))
{
interconnected.push(*edge);
}
}

interconnected
})
.max_by(|a, b| a.len().cmp(&b.len()))
.ok_or_else(|| AocError::logic("No groups found"))?;

largest.sort();
let password = largest.join(",");

Ok(password)
}
}

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

#[test]
fn it_solves_part1_example() {
assert_eq!(
Day23.part_1(
"kh-tc\n\
qp-kh\n\
de-cg\n\
ka-co\n\
yn-aq\n\
qp-ub\n\
cg-tb\n\
vc-aq\n\
tb-ka\n\
wh-tc\n\
yn-cg\n\
kh-ub\n\
ta-co\n\
de-co\n\
tc-td\n\
tb-wq\n\
wh-td\n\
ta-ka\n\
td-qp\n\
aq-cg\n\
wq-ub\n\
ub-vc\n\
de-ta\n\
wq-aq\n\
wq-vc\n\
wh-yn\n\
ka-de\n\
kh-ta\n\
co-tc\n\
wh-qp\n\
tb-vc\n\
td-yn"
),
Ok(7)
);
}

#[test]
fn it_solves_part2_example() {
assert_eq!(
Day23.part_2(
"kh-tc\n\
qp-kh\n\
de-cg\n\
ka-co\n\
yn-aq\n\
qp-ub\n\
cg-tb\n\
vc-aq\n\
tb-ka\n\
wh-tc\n\
yn-cg\n\
kh-ub\n\
ta-co\n\
de-co\n\
tc-td\n\
tb-wq\n\
wh-td\n\
ta-ka\n\
td-qp\n\
aq-cg\n\
wq-ub\n\
ub-vc\n\
de-ta\n\
wq-aq\n\
wq-vc\n\
wh-yn\n\
ka-de\n\
kh-ta\n\
co-tc\n\
wh-qp\n\
tb-vc\n\
td-yn"
),
Ok(String::from("co,de,ka,ta"))
);
}
}
8 changes: 4 additions & 4 deletions aoc-solver/src/y2024/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ pub mod day19;
pub mod day20;
pub mod day21;
pub mod day22;
// pub mod day23;
pub mod day23;
// pub mod day24;
// pub mod day25;

pub const MAX_DAYS: u8 = 22;
pub const MAX_DAYS: u8 = 23;

pub struct Y2024;

Expand Down Expand Up @@ -55,7 +55,7 @@ impl Solver for Y2024 {
20 => day20::Day20.run(input, 20, 2024),
21 => day21::Day21.run(input, 21, 2024),
22 => day22::Day22.run(input, 22, 2024),
// 23 => day23::Day23.run(input, 23, 2024),
23 => day23::Day23.run(input, 23, 2024),
// 24 => day24::Day24.run(input, 24, 2024),
// 25 => day25::Day25.run(input, 25, 2024),
_ => vec![String::from("Solution not implemented (yet?)")],
Expand Down Expand Up @@ -97,7 +97,7 @@ impl Solver for Y2024 {
20 => include_str!("./day20.rs"),
21 => include_str!("./day21.rs"),
22 => include_str!("./day22.rs"),
// 23 => include_str!("./day23.rs"),
23 => include_str!("./day23.rs"),
// 24 => include_str!("./day24.rs"),
// 25 => include_str!("./day25.rs"),
_ => unimplemented!(),
Expand Down
1 change: 1 addition & 0 deletions aoc-web/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub fn header(props: &HeaderProps) -> Html {
<NavLink route={Route::Solution { year: 2024, day: 20 }} current={props.route.clone()} text={"20"}/>
<NavLink route={Route::Solution { year: 2024, day: 21 }} current={props.route.clone()} text={"21"}/>
<NavLink route={Route::Solution { year: 2024, day: 22 }} current={props.route.clone()} text={"22"}/>
<NavLink route={Route::Solution { year: 2024, day: 23 }} current={props.route.clone()} text={"23"}/>
</>
}
},
Expand Down
Loading

0 comments on commit 96d7fe8

Please sign in to comment.