From 0171ee6758d1a14751cadb889b3d7e09539b4585 Mon Sep 17 00:00:00 2001 From: Ishan Jain Date: Wed, 6 Dec 2023 13:13:11 +0530 Subject: [PATCH] day6: optimized with quadratic inequality --- src/day6/1.rs | 57 +++++++++++++++++++++++++++++++++------------------ src/day6/2.rs | 54 ++++++++++++++++++++++++++---------------------- 2 files changed, 66 insertions(+), 45 deletions(-) diff --git a/src/day6/1.rs b/src/day6/1.rs index a8ea166..492256c 100644 --- a/src/day6/1.rs +++ b/src/day6/1.rs @@ -2,48 +2,65 @@ extern crate test; -const INPUTS: [&str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")]; +const INPUTS: [&[u8]; 2] = [ + include_bytes!("./sample.txt"), + include_bytes!("./input.txt"), +]; -fn process(data: &str) -> i64 { - let mut data = data.lines(); +fn process(data: &[u8]) -> u64 { + let mut data = data.split(|&x| x == b'\n'); let time = data .next() .map(|line| { - line.split(' ') - .filter(|x| !x.is_empty()) + line.split(|&x| x == b' ') .skip(1) - .map(|x| x.parse::().unwrap()) + .filter(|c| !c.is_empty()) + .map(|c| { + let mut num = 0; + let mut mul = 1; + + for c in c.iter().rev() { + num += (c - b'0') as u64 * mul; + mul *= 10; + } + + num as f32 + }) }) .unwrap(); let distance = data .next() .map(|line| { - line.split(' ') - .filter(|x| !x.is_empty()) + line.split(|&x| x == b' ') .skip(1) - .map(|x| x.parse::().unwrap()) + .filter(|c| !c.is_empty()) + .map(|c| { + let mut num = 0; + let mut mul = 1; + + for c in c.iter().rev() { + num += (c - b'0') as u64 * mul; + mul *= 10; + } + + num as f32 + }) }) .unwrap(); let mut answer = 1; for (time, dist) in time.zip(distance) { - let mut ways = 0; + let mut root1 = (time + (time * time - 4.0 * dist).sqrt()) / 2.0; + let root2 = (time - (time * time - 4.0 * dist).sqrt()) / 2.0; - for t in 0..time { - let time_left = time - t; - let speed = t; - - let dist_traveled = time_left * speed; - - if dist_traveled > dist { - ways += 1; - } + if root1.fract() == 0.0 && root2.fract() == 0.0 { + root1 -= 1.0; } - answer *= ways; + answer *= root1 as u64 - root2 as u64; } answer diff --git a/src/day6/2.rs b/src/day6/2.rs index d464a6b..a610d83 100644 --- a/src/day6/2.rs +++ b/src/day6/2.rs @@ -2,42 +2,46 @@ extern crate test; -const INPUTS: [&str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")]; +const INPUTS: [&[u8]; 2] = [ + include_bytes!("./sample.txt"), + include_bytes!("./input.txt"), +]; -fn process(data: &str) -> i64 { - let mut data = data.lines(); +fn process(data: &[u8]) -> u64 { + let mut data = data.split(|&x| x == b'\n'); - let time: u64 = data + let time: f64 = data .next() .map(|line| { - let x: String = line.chars().filter(|c| c.is_numeric()).collect(); - - x.parse::().unwrap() + let mut num = 0; + let mut mul = 1; + for c in line.iter().filter(|c| c.is_ascii_digit()).rev() { + num += (c - b'0') as u64 * mul; + mul *= 10; + } + num as f64 }) - .unwrap_or(0); - let distance: u64 = data + .unwrap_or(0.0); + let distance: f64 = data .next() .map(|line| { - let x: String = line.chars().filter(|c| c.is_numeric()).collect(); - - x.parse::().unwrap() + let mut num = 0; + let mut mul = 1; + for c in line.iter().filter(|c| c.is_ascii_digit()).rev() { + num += (c - b'0') as u64 * mul; + mul *= 10; + } + num as f64 }) - .unwrap_or(0); + .unwrap_or(0.0); - let mut answer = 0; - - for t in 0..time { - let time_left = time - t; - let speed = t; - - let dist_traveled = time_left * speed; - - if dist_traveled > distance { - answer += 1; - } + let mut root1 = (time + (time.powi(2) - 4.0 * distance).sqrt()) / 2.0; + let root2 = (time - (time.powi(2) - 4.0 * distance).sqrt()) / 2.0; + if root1.fract() == 0.0 && root2.fract() == 0.0 { + root1 -= 1.0; } - answer + root1 as u64 - root2 as u64 } fn main() {