From: Greg Burri Date: Sun, 1 Dec 2019 20:21:28 +0000 (+0100) Subject: Add time measurements. X-Git-Url: https://git.euphorik.ch/?a=commitdiff_plain;h=462662b908d8b272e8dfd974738ce06872b252a3;p=advent_of_code_2019.git Add time measurements. --- diff --git a/src/common.rs b/src/common.rs index 4893112..24c0c32 100644 --- a/src/common.rs +++ b/src/common.rs @@ -2,5 +2,5 @@ use std::fs; use std::path::Path; pub fn read_list_of_numbers>(file: P) -> Vec { - fs::read_to_string(file).unwrap().split("\n").map(|line| line.parse::().unwrap()).collect() + fs::read_to_string(file).unwrap().lines().map(|line| line.parse::().unwrap()).collect() } \ No newline at end of file diff --git a/src/day01.rs b/src/day01.rs index 1039494..d62a298 100644 --- a/src/day01.rs +++ b/src/day01.rs @@ -6,11 +6,11 @@ pub fn sum_mass_to_fuel_2(masses: &[i32]) -> i32 { masses.iter().fold(0, |sum, mass| sum + mass_to_fuel_2(*mass)) } -pub fn mass_to_fuel(mass: i32) -> i32 { +fn mass_to_fuel(mass: i32) -> i32 { mass / 3 - 2 } -pub fn mass_to_fuel_2(mass: i32) -> i32 { +fn mass_to_fuel_2(mass: i32) -> i32 { let mut sum = 0; let mut current_mass = mass; loop { diff --git a/src/main.rs b/src/main.rs index 509a1e5..5592192 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,32 +1,38 @@ use std::env; +use std::time::Instant; mod day01; mod common; -fn day01() { +fn day01() -> String { let masses = common::read_list_of_numbers("data/day01.input"); - println!("Day01 part1: {}", day01::sum_mass_to_fuel(&masses)); - println!(" part2: {}", day01::sum_mass_to_fuel_2(&masses)); + format!("part1: {}, part2: {}", day01::sum_mass_to_fuel(&masses), day01::sum_mass_to_fuel_2(&masses)) } -fn do_day(n: i32) { - match n { - 1 => day01 (), - _ => panic!("Unknown day: {}", n) - } +fn do_day(days: &Vec String>, day: usize) { + let now = Instant::now(); + println!("Result of day {}: {} (time: {} μs)", day, days[day - 1](), now.elapsed().as_micros()); } fn main() { println!("https://adventofcode.com/2019"); + let days: Vec String> = vec!( + day01 + ); + let args: Vec = env::args().skip(1).collect(); - if args.iter().count() == 0 { - println!("ASD"); + // No argument -> execute all day problems. + if args.len() == 0 { + for i in 1..=days.len() { + do_day(&days, i) + } } else { for arg in args { - do_day(arg.parse::().unwrap()); - //println!("{}", arg); + let day = arg.parse::().unwrap(); + if day > days.len() { panic!("Unknown day: {}", day) } + do_day(&days, day) } } }