Skip to content
This repository has been archived by the owner on Aug 1, 2024. It is now read-only.

Commit

Permalink
day 25
Browse files Browse the repository at this point in the history
  • Loading branch information
jpyamamoto committed Jan 20, 2024
1 parent b53fb7c commit ce5a46d
Show file tree
Hide file tree
Showing 4 changed files with 238 additions and 2 deletions.
166 changes: 165 additions & 1 deletion 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ itertools = "0.12.0"
num = "0.4.1"
ndarray = "0.15.6"
ndarray-linalg = { version = "0.16", features = ["openblas-static"] }
rustworkx-core = "0.13.2"
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.
| [Day 22](./src/bin/22.rs) | `9.5ms` | `17.6ms` |
| [Day 23](./src/bin/23.rs) | `2.3ms` | `1.7s` |
| [Day 24](./src/bin/24.rs) | `20.1ms` | `154.2µs` |
| [Day 25](./src/bin/25.rs) | `380.2ms` | `-` |

**Total: 48331.47ms**
**Total: 48711.67ms**
<!--- benchmarking table --->

---
Expand Down
70 changes: 70 additions & 0 deletions src/bin/25.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use std::convert::Infallible;
use std::collections::HashMap;
use rustworkx_core::connectivity::stoer_wagner_min_cut;
use rustworkx_core::petgraph::Undirected;
use rustworkx_core::petgraph::csr::Csr;

advent_of_code::solution!(25);

type Graph<'a> = Csr<&'a str, (), Undirected>;

fn parse_wiring(input: &str) -> (&str, Vec<&str>) {
let (key, wires) = input.split_once(": ").unwrap();
(key, wires.split_ascii_whitespace().collect())
}

fn parse(input: &str) -> Graph {
let wires = input.lines()
.map(|l| parse_wiring(l))
.collect::<Vec<_>>();

let mut nodes: HashMap<&str, u32> = HashMap::new();
let mut graph = Csr::new();

for (wire, _) in wires.iter() {
nodes.insert(wire, graph.add_node(*wire));
}

for (wire, conns) in wires {
let wire_idx = nodes[wire];

for conn in conns {
let conn_idx = nodes.entry(conn).or_insert_with(|| graph.add_node(conn));
graph.add_edge(wire_idx, *conn_idx, ());
}
}

graph
}

pub fn part_one(input: &str) -> Option<u32> {
let graph = parse(input);

let (_, connected_set) = stoer_wagner_min_cut(&graph, |_| Ok::<i32, Infallible>(1)).unwrap().unwrap();

let size1 = connected_set.len();
let size2 = graph.node_count() - size1;

Some((size1 * size2) as u32)
}

pub fn part_two(_input: &str) -> Option<u32> {
None
}

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

#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(54));
}

#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, None);
}
}

0 comments on commit ce5a46d

Please sign in to comment.