solution: day 6 clean

This commit is contained in:
Matej Janezic 2023-12-06 09:18:29 +01:00
parent 00000130fa
commit 00000140d3
Signed by: janezicmatej
GPG Key ID: 4298E230ED37B2C0
1 changed files with 23 additions and 19 deletions

View File

@ -1,5 +1,3 @@
use aoc::parsers::to_vec;
fn win_options((time, distance): (u64, u64)) -> u64 {
let discriminant = ((time.pow(2) - 4 * distance) as f64).sqrt();
@ -14,28 +12,34 @@ fn win_options((time, distance): (u64, u64)) -> u64 {
}
pub fn part_one(input: &str) -> Option<u64> {
let (upt, upd) = input.split_once('\n')?;
let time: Vec<u64> = to_vec(upt.strip_prefix("Time: ")?, ' ');
let distance: Vec<u64> = to_vec(upd.strip_prefix("Distance: ")?, ' ');
let [time, distance] = input
.lines()
.map(|l| {
l.split_whitespace()
.skip(1)
.filter_map(|n| n.parse().ok())
.collect::<Vec<u64>>()
})
.collect::<Vec<_>>()
.try_into()
.ok()?;
Some(time.into_iter().zip(distance).map(win_options).product())
}
pub fn part_two(input: &str) -> Option<u64> {
let (upt, upd) = input.split_once('\n')?;
let time: u64 = upt
.strip_prefix("Time: ")?
.split(' ')
.flat_map(|x| x.chars())
.collect::<String>()
.parse()
.ok()?;
let distance: u64 = upd
.strip_prefix("Distance: ")?
.split(' ')
.flat_map(|x| x.chars())
.collect::<String>()
.parse()
let [time, distance] = input
.lines()
.filter_map(|l| {
l.split_whitespace()
.skip(1)
.flat_map(|x| x.chars())
.collect::<String>()
.parse()
.ok()
})
.collect::<Vec<_>>()
.try_into()
.ok()?;
Some(win_options((time, distance)))