Skip to content

Commit

Permalink
[2023] Complete day 25
Browse files Browse the repository at this point in the history
  • Loading branch information
connorslade committed Dec 25, 2023
1 parent f131f8f commit 3163fe5
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 3 deletions.
93 changes: 90 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Thank you to [Eric Wastl](http://was.tl) for running this incredible yearly even
- [Day 22: Sand Slabs](aoc_2023/src/day_22.rs)
- [Day 23: A Long Walk](aoc_2023/src/day_23.rs)
- [Day 24: Never Tell Me The Odds](aoc_2023/src/day_24.rs)
- [Day 25: Snowverload](aoc_2023/src/day_25.rs)
<!-- MARKER -->

## [2022](https://adventofcode.com/2022) [![aoc_2022](https://github.com/Basicprogrammer10/advent-of-code/actions/workflows/aoc_2022.yml/badge.svg)](https://github.com/Basicprogrammer10/advent-of-code/actions/workflows/aoc_2022.yml)
Expand Down
1 change: 1 addition & 0 deletions aoc_2023/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ once_cell = "1.18.0"
polynomial = "0.2.6"
rayon = "1.8.0"
regex = "1.10.2"
rustworkx-core = "0.13.2"
83 changes: 83 additions & 0 deletions aoc_2023/src/day_25.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use std::collections::HashMap;

use common::{Answer, Solution};
use rustworkx_core::{
connectivity::stoer_wagner_min_cut,
petgraph::{graph::UnGraph, Graph, Undirected},
};

pub struct Day25;

impl Solution for Day25 {
fn name(&self) -> &'static str {
"Snowverload"
}

fn part_a(&self, input: &str) -> Answer {
let wires = parse(input);

let total = wires.wire.node_count();
let (len, side) = stoer_wagner_min_cut(&wires.wire, |_| Ok::<i32, ()>(1))
.unwrap()
.unwrap();

assert_eq!(len, 3);
((total - side.len()) * side.len()).into()
}

fn part_b(&self, _input: &str) -> Answer {
Answer::Unimplemented
}
}

struct Wires<'a> {
wire: Graph<&'a str, (), Undirected>,
}

fn parse(input: &str) -> Wires {
let mut wire = UnGraph::new_undirected();

let mut nodes = HashMap::new();

for line in input.lines() {
let mut parts = line.split(": ");
let key = parts.next().unwrap();
let values = parts.next().unwrap().split_whitespace();

let node = *nodes.entry(key).or_insert_with(|| wire.add_node(key));
for value in values {
let value = *nodes.entry(value).or_insert_with(|| wire.add_node(value));
wire.add_edge(node, value, ());
}
}
Wires { wire }
}

#[cfg(test)]
mod test {
use common::Solution;
use indoc::indoc;

use super::Day25;

const CASE: &str = indoc! {"
jqt: rhn xhk nvd
rsh: frs pzl lsr
xhk: hfx
cmg: qnr nvd lhk bvb
rhn: xhk bvb hfx
bvb: xhk hfx
pzl: lsr hfx nvd
qnr: nvd
ntq: jqt hfx bvb xhk
nvd: lhk
lsr: lhk
rzs: qnr cmg lsr rsh
frs: qnr lhk lsr
"};

#[test]
fn part_a() {
assert_eq!(Day25.part_a(CASE), 54.into());
}
}
2 changes: 2 additions & 0 deletions aoc_2023/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ mod day_21;
mod day_22;
mod day_23;
mod day_24;
mod day_25;
// [import_marker]

#[rustfmt::skip]
Expand Down Expand Up @@ -54,5 +55,6 @@ pub const ALL: &[&dyn Solution] = &[
&day_22::Day22,
&day_23::Day23,
&day_24::Day24,
&day_25::Day25,
// [list_marker]
];

0 comments on commit 3163fe5

Please sign in to comment.