From e078a3c6a711f86d80899bfe09cce80b30e5f95f Mon Sep 17 00:00:00 2001 From: Connor Slade Date: Mon, 25 Dec 2023 00:55:04 -0500 Subject: [PATCH] [2023] Day 25: Add petgraph as dep --- Cargo.lock | 1 + aoc_2023/Cargo.toml | 1 + aoc_2023/src/day_25.rs | 19 ++++++++++++------- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c1f758d..4356ff8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -152,6 +152,7 @@ dependencies = [ "nd_vec 0.4.0", "num-traits", "once_cell", + "petgraph", "polynomial", "rayon", "regex", diff --git a/aoc_2023/Cargo.toml b/aoc_2023/Cargo.toml index 9110481..b3dd116 100644 --- a/aoc_2023/Cargo.toml +++ b/aoc_2023/Cargo.toml @@ -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" diff --git a/aoc_2023/src/day_25.rs b/aoc_2023/src/day_25.rs index 4709003..b8e17d6 100644 --- a/aoc_2023/src/day_25.rs +++ b/aoc_2023/src/day_25.rs @@ -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; @@ -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, ()); } }