day6: added
This commit is contained in:
parent
dc62a5f0ed
commit
9787958cc4
|
@ -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);
|
||||
});
|
||||
}
|
||||
|
|
|
@ -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
2
src/day6/input.txt
Normal file
|
@ -0,0 +1,2 @@
|
|||
Time: 48 98 90 83
|
||||
Distance: 390 1103 1112 1360
|
2
src/day6/sample.txt
Normal file
2
src/day6/sample.txt
Normal file
|
@ -0,0 +1,2 @@
|
|||
Time: 7 15 30
|
||||
Distance: 9 40 200
|
Loading…
Reference in New Issue
Block a user