Skip to content

Commit 6f62f40

Browse files
committed
2023 rust
1 parent d18ee44 commit 6f62f40

File tree

5 files changed

+87
-5
lines changed

5 files changed

+87
-5
lines changed

2023/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@
22

33
[workspace]
44
members = ["day*"]
5-
exclude = ["day25"]
65

76
resolver = "2"

2023/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
![AoC2023](https://img.shields.io/badge/Advent_of_Code-2023-8A2BE2)
44
![Stars: 50](https://img.shields.io/badge/Stars-50⭐-blue)
5-
![Rust: 24](https://img.shields.io/badge/Rust-24-cyan?logo=Rust)
5+
![Rust: 25](https://img.shields.io/badge/Rust-25-cyan?logo=Rust)
66
![Python: 11](https://img.shields.io/badge/Python-11-cyan?logo=Python)
77

88
## 2023 ([Calendar](https://adventofcode.com/2023)) ([Solutions](../2023/)) : 50⭐
@@ -33,4 +33,4 @@ Puzzle |
3333
[Day 22: Sand Slabs](https://adventofcode.com/2023/day/22) | ⭐⭐ | [![Rust](../scripts/assets/rust.png)](../2023/day22/day22.rs)
3434
[Day 23: A Long Walk](https://adventofcode.com/2023/day/23) | ⭐⭐ | [![Rust](../scripts/assets/rust.png)](../2023/day23/day23.rs)
3535
[Day 24: Never Tell Me The Odds](https://adventofcode.com/2023/day/24) | ⭐⭐ | [![Rust](../scripts/assets/rust.png)](../2023/day24/day24.rs) [![Python](../scripts/assets/python.png)](../2023/day24/day24.py)
36-
[Day 25: Snowverload](https://adventofcode.com/2023/day/25) | ⭐⭐ | [![Python](../scripts/assets/python.png)](../2023/day25/day25.py)
36+
[Day 25: Snowverload](https://adventofcode.com/2023/day/25) | ⭐⭐ | [![Rust](../scripts/assets/rust.png)](../2023/day25/day25.rs) [![Python](../scripts/assets/python.png)](../2023/day25/day25.py)

2023/day25/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "day25"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
aoc = { path = "../../aoc" }
8+
rustworkx-core = "*"
9+
10+
[[bin]]
11+
name = "day25"
12+
path = "day25.rs"

2023/day25/day25.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//! [Day 25: Snowverload](https://adventofcode.com/2023/day/25)
2+
3+
use rustworkx_core::connectivity::stoer_wagner_min_cut;
4+
use rustworkx_core::petgraph::graph::Graph;
5+
use rustworkx_core::petgraph::Undirected;
6+
use std::collections::HashMap;
7+
8+
type G = Graph<(), (), Undirected>;
9+
10+
struct Puzzle {
11+
graph: G,
12+
}
13+
14+
impl Puzzle {
15+
fn new() -> Self {
16+
Self {
17+
graph: G::new_undirected(),
18+
}
19+
}
20+
21+
/// Get the puzzle input.
22+
fn configure(&mut self, path: &str) {
23+
let data = std::fs::read_to_string(path).unwrap();
24+
25+
let mut nodes = HashMap::new();
26+
27+
for line in data.lines() {
28+
let (node, cnx) = line.split_once(": ").unwrap();
29+
let connections = cnx.split_ascii_whitespace().collect::<Vec<_>>();
30+
31+
let node_index = *nodes.entry(node).or_insert_with(|| self.graph.add_node(()));
32+
33+
for connection in connections {
34+
let connection_index = *nodes
35+
.entry(connection)
36+
.or_insert_with(|| self.graph.add_node(()));
37+
38+
self.graph.add_edge(node_index, connection_index, ());
39+
}
40+
}
41+
}
42+
43+
/// Solve part one.
44+
fn part1(&mut self) -> usize {
45+
let min_cut = stoer_wagner_min_cut(&self.graph, |_| Ok::<u32, ()>(1));
46+
47+
let (_, edges) = min_cut.unwrap().unwrap();
48+
49+
(self.graph.node_count() - edges.len()) * edges.len()
50+
}
51+
}
52+
53+
fn main() {
54+
let args = aoc::parse_args();
55+
let mut puzzle = Puzzle::new();
56+
puzzle.configure(args.path.as_str());
57+
println!("{}", puzzle.part1());
58+
}
59+
60+
/// Test from puzzle input
61+
#[cfg(test)]
62+
mod test {
63+
use super::*;
64+
65+
#[test]
66+
fn test01() {
67+
let mut puzzle = Puzzle::new();
68+
puzzle.configure("test.txt");
69+
assert_eq!(puzzle.part1(), 54);
70+
}
71+
}

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# [Advent of Code](https://adventofcode.com) in Rust 🦀
22

33
![Stars: 492](https://img.shields.io/badge/Stars-492⭐-blue)
4-
![Rust: 196](https://img.shields.io/badge/Rust-196-cyan?logo=Rust)
4+
![Rust: 197](https://img.shields.io/badge/Rust-197-cyan?logo=Rust)
55
![Python: 121](https://img.shields.io/badge/Python-121-cyan?logo=Python)
66

77
Solutions of [Advent of Code](https://adventofcode.com/) in [Rust](https://www.rust-lang.org), and sometimes in [Python](https://www.python.org/) and other languages.
@@ -39,7 +39,7 @@ Puzzle | Stars |
3939
Calendar | Solutions | Stars | Rust | Python | 🎄
4040
-------- | --------- | ----- | ---- | ------ | --
4141
[Advent of Code 2024](https://adventofcode.com/2024) | [Solutions](2024/README.md) | 42⭐ | 21 | 8 | 3
42-
[Advent of Code 2023](https://adventofcode.com/2023) | [Solutions](2023/README.md) | 50⭐ | 24 | 11 | 1
42+
[Advent of Code 2023](https://adventofcode.com/2023) | [Solutions](2023/README.md) | 50⭐ | 25 | 11 | 1
4343
[Advent of Code 2022](https://adventofcode.com/2022) | [Solutions](2022/README.md) | 50⭐ | 24 | 18 |
4444
[Advent of Code 2021](https://adventofcode.com/2021) | [Solutions](2021/README.md) | 50⭐ | 23 | 12 |
4545
[Advent of Code 2020](https://adventofcode.com/2020) | [Solutions](2020/README.md) | 50⭐ | 7 | 23 |

0 commit comments

Comments
 (0)