use std::path::Path;\r
\r
pub fn read_list_of_numbers<P: AsRef<Path>>(file: P) -> Vec<i32> {\r
- fs::read_to_string(file).unwrap().split("\n").map(|line| line.parse::<i32>().unwrap()).collect()\r
+ fs::read_to_string(file).unwrap().lines().map(|line| line.parse::<i32>().unwrap()).collect()\r
}
\ No newline at end of file
masses.iter().fold(0, |sum, mass| sum + mass_to_fuel_2(*mass))\r
}\r
\r
-pub fn mass_to_fuel(mass: i32) -> i32 {\r
+fn mass_to_fuel(mass: i32) -> i32 {\r
mass / 3 - 2\r
}\r
\r
-pub fn mass_to_fuel_2(mass: i32) -> i32 {\r
+fn mass_to_fuel_2(mass: i32) -> i32 {\r
let mut sum = 0;\r
let mut current_mass = mass;\r
loop {\r
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<fn() -> 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<fn() -> String> = vec!(
+ day01
+ );
+
let args: Vec<String> = 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::<i32>().unwrap());
- //println!("{}", arg);
+ let day = arg.parse::<usize>().unwrap();
+ if day > days.len() { panic!("Unknown day: {}", day) }
+ do_day(&days, day)
}
}
}