solution: day 6 clean

This commit is contained in:
Matej Janezic 2023-12-06 09:18:29 +01:00
parent 00000130fa
commit bdaae423b4
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 { fn win_options((time, distance): (u64, u64)) -> u64 {
let discriminant = ((time.pow(2) - 4 * distance) as f64).sqrt(); 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> { pub fn part_one(input: &str) -> Option<u64> {
let (upt, upd) = input.split_once('\n')?; let [time, distance] = input
let time: Vec<u64> = to_vec(upt.strip_prefix("Time: ")?, ' '); .lines()
let distance: Vec<u64> = to_vec(upd.strip_prefix("Distance: ")?, ' '); .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()) Some(time.into_iter().zip(distance).map(win_options).product())
} }
pub fn part_two(input: &str) -> Option<u64> { pub fn part_two(input: &str) -> Option<u64> {
let (upt, upd) = input.split_once('\n')?; let [time, distance] = input
let time: u64 = upt .lines()
.strip_prefix("Time: ")? .filter_map(|l| {
.split(' ') l.split_whitespace()
.flat_map(|x| x.chars()) .skip(1)
.collect::<String>() .flat_map(|x| x.chars())
.parse() .collect::<String>()
.ok()?; .parse()
let distance: u64 = upd .ok()
.strip_prefix("Distance: ")? })
.split(' ') .collect::<Vec<_>>()
.flat_map(|x| x.chars()) .try_into()
.collect::<String>()
.parse()
.ok()?; .ok()?;
Some(win_options((time, distance))) Some(win_options((time, distance)))