From 9787958cc472e968c85528b4b7c5bcb042938eef Mon Sep 17 00:00:00 2001 From: Ishan Jain Date: Wed, 6 Dec 2023 10:46:11 +0530 Subject: [PATCH] day6: added --- src/day6/1.rs | 65 ++++++++++++++++++++++++++++++++++++++++++++- src/day6/2.rs | 56 +++++++++++++++++++++++++++++++++++++- src/day6/input.txt | 2 ++ src/day6/sample.txt | 2 ++ 4 files changed, 123 insertions(+), 2 deletions(-) create mode 100644 src/day6/input.txt create mode 100644 src/day6/sample.txt diff --git a/src/day6/1.rs b/src/day6/1.rs index b51073e..a8ea166 100644 --- a/src/day6/1.rs +++ b/src/day6/1.rs @@ -1 +1,64 @@ -const INPUTS: [&'static str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")]; +#![feature(test)] + +extern crate test; + +const INPUTS: [&str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")]; + +fn process(data: &str) -> i64 { + let mut data = data.lines(); + + let time = data + .next() + .map(|line| { + line.split(' ') + .filter(|x| !x.is_empty()) + .skip(1) + .map(|x| x.parse::().unwrap()) + }) + .unwrap(); + + let distance = data + .next() + .map(|line| { + line.split(' ') + .filter(|x| !x.is_empty()) + .skip(1) + .map(|x| x.parse::().unwrap()) + }) + .unwrap(); + + let mut answer = 1; + + for (time, dist) in time.zip(distance) { + let mut ways = 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; + } + } + + answer *= ways; + } + + answer +} + +fn main() { + for input in INPUTS.iter() { + println!("total = {}", process(input)); + } +} + +#[bench] +fn part1(b: &mut test::Bencher) { + b.iter(|| { + let v = process(INPUTS[1]); + test::black_box(v); + }); +} diff --git a/src/day6/2.rs b/src/day6/2.rs index b51073e..d464a6b 100644 --- a/src/day6/2.rs +++ b/src/day6/2.rs @@ -1 +1,55 @@ -const INPUTS: [&'static str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")]; +#![feature(test)] + +extern crate test; + +const INPUTS: [&str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")]; + +fn process(data: &str) -> i64 { + let mut data = data.lines(); + + let time: u64 = data + .next() + .map(|line| { + let x: String = line.chars().filter(|c| c.is_numeric()).collect(); + + x.parse::().unwrap() + }) + .unwrap_or(0); + let distance: u64 = data + .next() + .map(|line| { + let x: String = line.chars().filter(|c| c.is_numeric()).collect(); + + x.parse::().unwrap() + }) + .unwrap_or(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; + } + } + + answer +} + +fn main() { + for input in INPUTS.iter() { + println!("total = {}", process(input)); + } +} + +#[bench] +fn part1(b: &mut test::Bencher) { + b.iter(|| { + let v = process(INPUTS[1]); + test::black_box(v); + }); +} diff --git a/src/day6/input.txt b/src/day6/input.txt new file mode 100644 index 0000000..d3ee5ff --- /dev/null +++ b/src/day6/input.txt @@ -0,0 +1,2 @@ +Time: 48 98 90 83 +Distance: 390 1103 1112 1360 diff --git a/src/day6/sample.txt b/src/day6/sample.txt new file mode 100644 index 0000000..28f5ae9 --- /dev/null +++ b/src/day6/sample.txt @@ -0,0 +1,2 @@ +Time: 7 15 30 +Distance: 9 40 200