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

View File

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