day6: added

This commit is contained in:
Ishan Jain 2023-12-06 10:46:11 +05:30
parent dc62a5f0ed
commit 9787958cc4
Signed by: ishan
GPG Key ID: 0506DB2A1CC75C27
4 changed files with 123 additions and 2 deletions

View File

@ -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::<u64>().unwrap())
})
.unwrap();
let distance = data
.next()
.map(|line| {
line.split(' ')
.filter(|x| !x.is_empty())
.skip(1)
.map(|x| x.parse::<u64>().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);
});
}

View File

@ -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::<u64>().unwrap()
})
.unwrap_or(0);
let distance: u64 = data
.next()
.map(|line| {
let x: String = line.chars().filter(|c| c.is_numeric()).collect();
x.parse::<u64>().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);
});
}

2
src/day6/input.txt Normal file
View File

@ -0,0 +1,2 @@
Time: 48 98 90 83
Distance: 390 1103 1112 1360

2
src/day6/sample.txt Normal file
View File

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