Skip to content

Commit

Permalink
solved day21 part2
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippPolterauer committed Dec 27, 2023
1 parent 0ca43ef commit 2006a27
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 19 deletions.
152 changes: 148 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ default-run = "adventofcode"
name = "adventofcode"
path = "src/main.rs"

[dependencies]
nalgebra = "0.32.3"

[dependencies.clap]
version = "4.4.10"
features = ["derive"]
Expand Down
31 changes: 16 additions & 15 deletions src/days/day21.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use core::panic;
use std::collections::HashSet;
use std::fs::File;
use std::io::Write;
use nalgebra::{DMatrix, DVector};

use crate::util::*;

Expand Down Expand Up @@ -62,27 +63,27 @@ pub fn part2(input: &str) -> i64 {
let mut ys = Vec::new();
let mut xs = Vec::new();

for step in 1..4097 {

for step in 1..5000 {
front = take_step_inf(&matrix, &front);
if step % 2 == 1 {
if step%2==1{
front = front.difference(&odd).copied().collect();
odd.extend(front.iter());
}
if (step-65) % 262 == 0 {
xs.push(step);
ys.push(odd.len() as i32)
}
dbg!(step);
}

// Open or create a file for writing
let mut file = File::create("output.txt").unwrap();

// Integer values to write

// Iterate over the integers and write each one to a new line in the file
for (x,y) in xs.iter().zip(ys.iter()) {
writeln!(file, "{}, {}", x, y).unwrap();
}
let xs = DVector::from_vec(xs);
let ys = DVector::from_vec(ys).cast::<f64>();
let mut ones = xs.clone();
ones.fill(1);
let xs2 = xs.clone().component_mul(&xs);


0
let phi = DMatrix::from_columns(&[ones, xs, xs2]).cast::<f64>();
let pars = phi.svd(true,true).solve(&ys, 1e-13).unwrap();
let cnt = 26501365f64;
let data = DVector::from_column_slice(&[1f64, cnt, cnt*cnt]);
pars.tr_mul(&data).get(0).unwrap().round() as i64
}

0 comments on commit 2006a27

Please sign in to comment.