Fixed formatting

This commit is contained in:
Dreaded_X 2023-12-25 00:41:27 +01:00
parent fbd9e84f5f
commit 6233ade178
Signed by: Dreaded_X
GPG Key ID: 5A0CBFE3C3377FAA
3 changed files with 80 additions and 59 deletions

View File

@ -1,9 +1,6 @@
#![feature(iter_map_windows)]
#![feature(test)]
use std::{
cmp::max,
collections::VecDeque,
};
use std::{cmp::max, collections::VecDeque};
use anyhow::Result;
use aoc::Solver;

View File

@ -1,6 +1,10 @@
#![feature(test)]
use std::{fmt::Display, collections::{hash_map::DefaultHasher, HashMap}, hash::{Hash, Hasher}};
use std::{
collections::{hash_map::DefaultHasher, HashMap},
fmt::Display,
hash::{Hash, Hasher},
};
use anyhow::Result;
use aoc::Solver;

View File

@ -1,10 +1,10 @@
#![feature(test)]
extern crate nalgebra as na;
use std::{str::FromStr, convert::Infallible, collections::HashMap};
use std::{collections::HashMap, convert::Infallible, str::FromStr};
use anyhow::Result;
use aoc::Solver;
use na::{SMatrix, SVector};
use na::{Matrix6, Vector6};
// -- Runners --
fn main() -> Result<()> {
@ -56,7 +56,7 @@ struct Hailstone {
pz: f64,
vx: f64,
vy: f64,
vz: f64
vz: f64,
}
impl Hailstone {
@ -90,7 +90,10 @@ impl FromStr for Hailstone {
type Err = Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let parts: Vec<f64> = s.split([',', '@']).map(|part| part.trim().parse().unwrap()).collect();
let parts: Vec<f64> = s
.split([',', '@'])
.map(|part| part.trim().parse().unwrap())
.collect();
Ok(Hailstone {
px: parts[0],
@ -112,7 +115,11 @@ fn mode(numbers: &[usize]) -> usize {
println!("{occurrences:?}");
occurrences.into_iter().max_by_key(|&(_, count)| count).map(|(value, _)| value).unwrap()
occurrences
.into_iter()
.max_by_key(|&(_, count)| count)
.map(|(value, _)| value)
.unwrap()
}
// -- Solution --
@ -134,11 +141,19 @@ impl aoc::Solver for Day {
200000000000000.0..=400000000000000.0
};
hailstones.iter().enumerate().flat_map(|(index_a, a)| {
hailstones.iter().enumerate().filter(|(index_b, _)| index_b < &index_a).filter_map(|(_, b)| {
a.intersect_2d(b)
}).collect::<Vec<_>>()
}).filter(|&(x, y)| range.contains(&x) && range.contains(&y)).count()
hailstones
.iter()
.enumerate()
.flat_map(|(index_a, a)| {
hailstones
.iter()
.enumerate()
.filter(|(index_b, _)| index_b < &index_a)
.filter_map(|(_, b)| a.intersect_2d(b))
.collect::<Vec<_>>()
})
.filter(|&(x, y)| range.contains(&x) && range.contains(&y))
.count()
}
fn part2(input: &str) -> Self::Output2 {
@ -168,7 +183,8 @@ impl aoc::Solver for Day {
let j = 1;
// Due to numerical instability we run this with several different options for the third
// hailstone
let solutions: Vec<_> = (2..h.len()).map(|k| {
let solutions: Vec<_> = (2..h.len())
.map(|k| {
// Constant in the matrix
let c1 = h[i].vz - h[j].vz;
let c2 = h[i].py - h[j].py;
@ -185,35 +201,39 @@ impl aoc::Solver for Day {
let c12 = h[k].px - h[i].px;
// Setup the matrix
let matrix = SMatrix::<f64, 6, 6>::new(
0.0, c1, c3, 0.0, c4, c2,
-c1, 0.0, c5, -c4, 0.0, c6,
-c3, -c5, 0.0, -c2, -c6, 0.0,
0.0, c7, c9, 0.0, c10, c8,
-c7, 0.0, c11, -c10, 0.0, c12,
-c9, -c11, 0.0, -c8, -c12, 0.0
let matrix = Matrix6::new(
0.0, c1, c3, 0.0, c4, c2, -c1, 0.0, c5, -c4, 0.0, c6, -c3, -c5, 0.0, -c2, -c6,
0.0, 0.0, c7, c9, 0.0, c10, c8, -c7, 0.0, c11, -c10, 0.0, c12, -c9, -c11, 0.0,
-c8, -c12, 0.0,
);
// Get the inverse of the matrix
let inverse = matrix.try_inverse().unwrap();
// Constant on the rhs
let k1 = h[i].py*h[i].vz - h[j].py*h[j].vz + h[j].pz*h[j].vy - h[i].pz*h[i].vy;
let k2 = h[i].pz*h[i].vx - h[j].pz*h[j].vx + h[j].px*h[j].vz - h[i].px*h[i].vz;
let k3 = h[i].px*h[i].vy - h[j].px*h[j].vy + h[j].py*h[j].vx - h[i].py*h[i].vx;
let k4 = h[i].py*h[i].vz - h[k].py*h[k].vz + h[k].pz*h[k].vy - h[i].pz*h[i].vy;
let k5 = h[i].pz*h[i].vx - h[k].pz*h[k].vx + h[k].px*h[k].vz - h[i].px*h[i].vz;
let k6 = h[i].px*h[i].vy - h[k].px*h[k].vy + h[k].py*h[k].vx - h[i].py*h[i].vx;
let k1 =
h[i].py * h[i].vz - h[j].py * h[j].vz + h[j].pz * h[j].vy - h[i].pz * h[i].vy;
let k2 =
h[i].pz * h[i].vx - h[j].pz * h[j].vx + h[j].px * h[j].vz - h[i].px * h[i].vz;
let k3 =
h[i].px * h[i].vy - h[j].px * h[j].vy + h[j].py * h[j].vx - h[i].py * h[i].vx;
let k4 =
h[i].py * h[i].vz - h[k].py * h[k].vz + h[k].pz * h[k].vy - h[i].pz * h[i].vy;
let k5 =
h[i].pz * h[i].vx - h[k].pz * h[k].vx + h[k].px * h[k].vz - h[i].px * h[i].vz;
let k6 =
h[i].px * h[i].vy - h[k].px * h[k].vy + h[k].py * h[k].vx - h[i].py * h[i].vx;
// Put them into a vector
let k = SVector::<f64, 6>::new(k1, k2, k3, k4, k5, k6);
let k = Vector6::new(k1, k2, k3, k4, k5, k6);
// Calclate the solution
let solution = inverse * k;
// The sum of all elements of the starting position is the answer
(solution[0] + solution[1] + solution[2]).round() as usize
}).collect();
})
.collect();
// The most common solution is the actual solution
mode(&solutions)