feat: day 6

This commit is contained in:
Ashhhleyyy 2023-12-06 23:51:52 +00:00
parent 8cb6a0fee2
commit 1f7ed1bf1e
Signed by: ash
GPG key ID: 83B789081A0878FB
3 changed files with 52 additions and 0 deletions

2
.aoc-cache/6.txt Normal file
View file

@ -0,0 +1,2 @@
Time: 58 99 64 69
Distance: 478 2232 1019 1071

48
src/bin/day_6.rs Normal file
View file

@ -0,0 +1,48 @@
use aoc_2022::prelude::*;
use color_eyre::eyre::ContextCompat;
use std::fmt::Write;
type Input = Vec<(isize, isize)>;
fn parse(s: &str) -> Result<Input> {
let (time, distance) = s.split_once("\n")
.wrap_err("invalid format")?;
Ok(ints(time).into_iter().zip(ints(distance)).collect::<Vec<_>>())
}
fn race_calc((total_time, target_distance): (isize, isize)) -> usize {
let a = 1.0;
let b = -total_time as f64;
let c = target_distance as f64;
let v1 = (-b + (b*b - 4.0*a*c).sqrt()) / 2.0*a;
let v2 = (-b - (b*b - 4.0*a*c).sqrt()) / 2.0*a;
let min = v1.min(v2) + 0.00001;
let max = v1.max(v2) - 0.00001;
let min = min.ceil() as i64;
let max = max.floor() as i64;
(min..=max).count()
}
#[aoc(day = 6, parse = parse, test_cases = ["day_6.txt"])]
fn day_6(mut input: Input) -> Result<()> {
// Part 1
println!("Part one: ");
let result: usize = input.iter().copied().map(race_calc).product();
println!("{result}");
// Part 2
println!("Part two: ");
let mut total_time = String::new();
let mut target_distance = String::new();
for (t, d) in &input {
write!(total_time, "{t}")?;
write!(target_distance, "{d}")?;
}
let total_time = total_time.parse()?;
let target_distance = target_distance.parse()?;
println!("{total_time} {target_distance}");
let result = race_calc((total_time, target_distance));
println!("{result}");
Ok(())
}

2
test_cases/day_6.txt Normal file
View file

@ -0,0 +1,2 @@
Time: 7 15 30
Distance: 9 40 200