day2: performance and adopt new duration print
This commit is contained in:
parent
ec2651c3e4
commit
b24593a469
@ -11,7 +11,7 @@ fn get_input() -> InputIter {
|
|||||||
br.lines()
|
br.lines()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn duration_format(duration: &Duration) -> String {
|
fn duration_format(duration: Duration) -> String {
|
||||||
match duration.as_secs_f64() {
|
match duration.as_secs_f64() {
|
||||||
x if x > 1.0 => format!("{:.3}s", x),
|
x if x > 1.0 => format!("{:.3}s", x),
|
||||||
x if x > 0.010 => format!("{:.3}ms", x * 1e3),
|
x if x > 0.010 => format!("{:.3}ms", x * 1e3),
|
||||||
@ -20,23 +20,18 @@ fn duration_format(duration: &Duration) -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
let input = get_input();
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
let ans1 = problem1(get_input());
|
let ans1 = problem1(input);
|
||||||
let duration = start.elapsed();
|
let duration1 = start.elapsed();
|
||||||
println!(
|
println!("Problem 1 solution: {} [{}]", ans1, duration_format(duration1));
|
||||||
"Problem 1 solution: {} [{}]",
|
|
||||||
ans1,
|
|
||||||
duration_format(&duration)
|
|
||||||
);
|
|
||||||
|
|
||||||
|
let input = get_input();
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
let ans2 = problem2(get_input());
|
let ans2 = problem2(input);
|
||||||
let duration = start.elapsed();
|
let duration2 = start.elapsed();
|
||||||
println!(
|
println!("Problem 2 solution: {} [{}]", ans2, duration_format(duration2));
|
||||||
"Problem 2 solution: {} [{}]",
|
println!("Total duration: {}", duration_format(duration1 + duration2));
|
||||||
ans2,
|
|
||||||
duration_format(&duration)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Reports {
|
struct Reports {
|
||||||
@ -59,12 +54,21 @@ impl<T: BufRead> From<Lines<T>> for Reports {
|
|||||||
|
|
||||||
impl Reports {
|
impl Reports {
|
||||||
fn is_safe(report: &Vec<u64>) -> bool {
|
fn is_safe(report: &Vec<u64>) -> bool {
|
||||||
(report.iter().zip(report.iter().skip(1)).all(|(a, b)| a > b)
|
let mut ascending: bool = true;
|
||||||
|| report.iter().zip(report.iter().skip(1)).all(|(a, b)| a < b))
|
let mut descending: bool = true;
|
||||||
&& report
|
for (a, b) in report.iter().zip(report.iter().skip(1)) {
|
||||||
.iter()
|
if a > b {
|
||||||
.zip(report.iter().skip(1))
|
ascending = false
|
||||||
.all(|(a, b)| a.abs_diff(*b) >= 1 && a.abs_diff(*b) <= 3)
|
}
|
||||||
|
if a < b {
|
||||||
|
descending = false;
|
||||||
|
}
|
||||||
|
let ad = a.abs_diff(*b);
|
||||||
|
if !(ad >= 1 && ad <= 3) || (!ascending && !descending) {
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
fn count_safe(&self) -> u64 {
|
fn count_safe(&self) -> u64 {
|
||||||
self.reports.iter().filter(|report| Self::is_safe(report)).count() as u64
|
self.reports.iter().filter(|report| Self::is_safe(report)).count() as u64
|
||||||
|
Loading…
x
Reference in New Issue
Block a user