Skip to content

Commit

Permalink
[2023] Day 25: Add petgraph as dep
Browse files Browse the repository at this point in the history
  • Loading branch information
connorslade committed Dec 25, 2023
1 parent 3163fe5 commit e078a3c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
1 change: 1 addition & 0 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 aoc_2023/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ itertools = "0.12.0"
nd_vec = "0.4.0"
num-traits = "0.2.17"
once_cell = "1.18.0"
petgraph = "0.6.4"
polynomial = "0.2.6"
rayon = "1.8.0"
regex = "1.10.2"
Expand Down
19 changes: 12 additions & 7 deletions aoc_2023/src/day_25.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use std::collections::HashMap;

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

pub struct Day25;

Expand Down Expand Up @@ -35,18 +33,25 @@ struct Wires<'a> {
}

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

let mut nodes = HashMap::new();
fn get_node<'a>(
nodes: &mut HashMap<&'a str, NodeIndex>,
wire: &mut Graph<&'a str, (), Undirected>,
name: &'a str,
) -> NodeIndex {
*nodes.entry(name).or_insert_with(|| wire.add_node(name))
}

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));
let node = get_node(&mut nodes, &mut wire, key);
for value in values {
let value = *nodes.entry(value).or_insert_with(|| wire.add_node(value));
let value = get_node(&mut nodes, &mut wire, value);
wire.add_edge(node, value, ());
}
}
Expand Down

0 comments on commit e078a3c

Please sign in to comment.