Use Clap for arguments parsing
authorGreg Burri <greg.burri@gmail.com>
Tue, 13 Dec 2022 15:13:26 +0000 (16:13 +0100)
committerGreg Burri <greg.burri@gmail.com>
Tue, 13 Dec 2022 15:13:26 +0000 (16:13 +0100)
Add a -p for processing all days in parallel

Cargo.toml
src/days.rs
src/main.rs

index 3e0b2fe..3a6371a 100644 (file)
@@ -9,6 +9,8 @@ edition = "2021"
 [dependencies]
 itertools = "0.10"
 regex = "1"
+clap = { version = "4", features = ["derive"] }
+rayon = "1.6"
 
 [profile.release]
 opt-level = 3
index faa0216..9e999b3 100644 (file)
@@ -120,8 +120,8 @@ pub fn day11() -> String {
     let monkeys = day11::parse(&fs::read_to_string("data/day11.input").unwrap());
     format!(
         "part1: {}, part2: {}",
-        day11::run(&mut monkeys.clone(), 20, 3),
-        day11::run(&mut monkeys.clone(), 10000, 1)
+        day11::run::<3>(&mut monkeys.clone(), 20),
+        day11::run::<1>(&mut monkeys.clone(), 10000)
     )
 }
 
@@ -133,3 +133,12 @@ pub fn day12() -> String {
         day12::nb_steps(&heightmap, day12::Path::EndTo0Elevation)
     )
 }
+
+pub fn day13() -> String {
+    let signals = day13::parse(&fs::read_to_string("data/day13.input").unwrap());
+    format!(
+        "part1: {}, part2: {}",
+        day13::sum_indices_signals_in_the_right_order(&signals),
+        day13::product_indices_special_signals(&signals)
+    )
+}
index dc7720e..85ca7e3 100644 (file)
@@ -1,4 +1,7 @@
-use std::{env, time::Instant};
+use std::time::Instant;
+
+use clap::Parser;
+use rayon::prelude::*;
 
 mod day01;
 mod day02;
@@ -12,8 +15,19 @@ mod day09;
 mod day10;
 mod day11;
 mod day12;
+mod day13;
 mod days;
 
+#[derive(Parser, Debug)]
+#[command(author = "Greg Burri", version = "1.0", about = "Advent of Code 2022")]
+struct Args {
+    #[arg(index(1), exclusive(true))]
+    day: Option<usize>,
+
+    #[arg(short, long)]
+    parallel: bool,
+}
+
 fn main() {
     println!("https://adventofcode.com/2022");
 
@@ -30,27 +44,35 @@ fn main() {
         days::day10,
         days::day11,
         days::day12,
+        days::day13,
     ];
 
-    let args: Vec<String> = env::args().skip(1).collect();
+    let args = Args::parse();
 
-    // No argument -> execute all day problems.
-    if args.is_empty() {
-        let now = Instant::now();
-        for i in 1..=days.len() {
-            do_day(&days, i)
+    match args.day {
+        Some(day) => {
+            if day >= 1 && day <= days.len() {
+                do_day(&days, day)
+            } else {
+                println!("Unknown day: {}", day)
+            }
         }
-        println!(
-            "Time to execute all days: {}",
-            format_micros(now.elapsed().as_micros())
-        );
-    } else {
-        for arg in args {
-            match arg.parse::<usize>() {
-                Ok(day) if day >= 1 && day <= days.len() => do_day(&days, day),
-                Ok(day) => println!("Unknown day: {}", day),
-                Err(error) => println!("Unable to parse day number: \"{}\", error: {}", arg, error),
+        // No argument -> execute all day problems.
+        None => {
+            let now = Instant::now();
+
+            if args.parallel {
+                (1..=days.len())
+                    .into_par_iter()
+                    .for_each(|d| do_day(&days, d));
+            } else {
+                (1..=days.len()).for_each(|d| do_day(&days, d));
             }
+
+            println!(
+                "Time to execute all days: {}",
+                format_micros(now.elapsed().as_micros())
+            );
         }
     }
 }