diff --git a/.aoc_tiles/tiles/2025/08.png b/.aoc_tiles/tiles/2025/08.png new file mode 100644 index 0000000..8c4293a Binary files /dev/null and b/.aoc_tiles/tiles/2025/08.png differ diff --git a/README.md b/README.md index 7a45bf0..7c943c9 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@

- 2025 - 14 ⭐ - Rust + 2025 - 16 ⭐ - Rust

@@ -23,4 +23,7 @@ + + + diff --git a/src/day8.rs b/src/day8.rs index 9486089..6e6f24e 100644 --- a/src/day8.rs +++ b/src/day8.rs @@ -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 { + 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,14 +74,7 @@ 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), - }; + self.merge_circuits(a_circuit, b_circuit); // both are already in circuits, merge them } } @@ -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()