day8: some cleanups

This commit is contained in:
2025-12-08 01:50:39 -08:00
parent c97f31e0fd
commit 09ca512fcc
3 changed files with 12 additions and 15 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.6 KiB

View File

@@ -1,6 +1,6 @@
<!-- AOC TILES BEGIN -->
<h1 align="center">
2025 - 14 ⭐ - Rust
2025 - 16 ⭐ - Rust
</h1>
<a href="src/day1.rs">
<img src=".aoc_tiles/tiles/2025/01.png" width="161px">
@@ -23,4 +23,7 @@
<a href="src/day7.rs">
<img src=".aoc_tiles/tiles/2025/07.png" width="161px">
</a>
<a href="src/day8.rs">
<img src=".aoc_tiles/tiles/2025/08.png" width="161px">
</a>
<!-- AOC TILES END -->

View File

@@ -10,7 +10,7 @@ struct Junction {
fn distance(a: &Junction, b: &Junction) -> f64 {
if a.pos == b.pos {
f64::MAX // ugh
0f64
} else {
(((a.pos.0 - b.pos.0).pow(2) + (a.pos.1 - b.pos.1).pow(2) + (a.pos.2 - b.pos.2).pow(2))
as f64)
@@ -52,12 +52,13 @@ struct Circuits {
}
impl Circuits {
fn find_circuit(&self, junction: usize) -> Option<usize> {
fn find_circuit(&self, junction: usize) -> usize {
self.circuits
.iter()
.enumerate()
.find(|(_i, c)| c.contains(&junction))
.map(|(i, _c)| i)
.unwrap()
}
fn merge_circuits(&mut self, a: usize, b: usize) {
if a == b {
@@ -73,15 +74,8 @@ impl Circuits {
let a_circuit = self.find_circuit(a);
let b_circuit = self.find_circuit(b);
match (a_circuit, b_circuit) {
(None, None) => self.circuits.push(vec![a, b]), // both are unconnected
(Some(a_circuit), Some(b_circuit)) => {
self.merge_circuits(a_circuit, b_circuit); // both are already in circuits, merge them
}
(Some(a_circuit), None) => self.circuits[a_circuit].push(b), // one is in a circuit, so add the other to the existing circuit
(None, Some(b_circuit)) => self.circuits[b_circuit].push(a),
};
}
}
#[aoc_generator(day8)]
@@ -89,8 +83,8 @@ fn parse(input: &str) -> Circuits {
let junctions = input.lines().map(|l| l.into()).collect_vec();
Circuits {
circuits: Vec::from_iter((0..junctions.len()).map(|i| vec![i])),
junctions,
circuits: Vec::new(),
}
}
@@ -100,8 +94,8 @@ fn part1_impl(input: &Circuits, n: usize) -> u64 {
.junctions
.iter()
.enumerate()
.combinations(2)
.map(|p| (p[0].0, p[1].0, p[0].1.distance(p[1].1)))
.tuple_combinations()
.map(|((a_pos, a), (b_pos, b))| (a_pos, b_pos, a.distance(b)))
.sorted_by(|a, b| a.2.partial_cmp(&b.2).unwrap())
.take(n)
{
@@ -127,7 +121,7 @@ fn part1(input: &Circuits) -> u64 {
#[aoc(day8, part2)]
fn part2(input: &Circuits) -> u64 {
let mut circuits = input.clone();
circuits.circuits = Vec::from_iter((0..circuits.junctions.len()).map(|i| vec![i]));
for (a, b, _d) in circuits
.junctions
.iter()