Skip to content

Commit c64abfd

Browse files
committed
Extract solution execution
1 parent 7949c60 commit c64abfd

File tree

1 file changed

+31
-27
lines changed

1 file changed

+31
-27
lines changed

src/main.rs

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@ use std::env::args;
44
use std::fs::read_to_string;
55
use std::time::{Duration, Instant};
66

7+
struct Solution {
8+
year: u32,
9+
day: u32,
10+
wrapper: fn(&str) -> (String, String),
11+
}
12+
713
fn main() {
814
// Parse command line options
915
let mut iter = args().flat_map(|arg| arg.iter_unsigned().collect::<Vec<u32>>());
1016
let (year, day) = (iter.next(), iter.next());
1117

18+
// Build list of all solutions.
1219
let solutions = [
1320
year2015(),
1421
year2016(),
@@ -22,33 +29,13 @@ fn main() {
2229
year2024(),
2330
];
2431

25-
// Filter solutions then pretty print output.
32+
// Run selected solutions.
2633
let (stars, duration) = solutions
27-
.into_iter()
34+
.iter()
2835
.flatten()
2936
.filter(|s| year.is_none_or(|y| y == s.year))
3037
.filter(|s| day.is_none_or(|d| d == s.day))
31-
.fold((0, Duration::ZERO), |(stars, duration), Solution { year, day, wrapper }| {
32-
let path = format!("input/year{year}/day{day:02}.txt");
33-
34-
if let Ok(data) = read_to_string(&path) {
35-
let instant = Instant::now();
36-
let (part1, part2) = wrapper(&data);
37-
let elapsed = instant.elapsed();
38-
39-
println!("{BOLD}{YELLOW}{year} Day {day}{RESET}");
40-
println!(" Part 1: {part1}");
41-
println!(" Part 2: {part2}");
42-
43-
(stars + 2, duration + elapsed)
44-
} else {
45-
eprintln!("{BOLD}{RED}{year} Day {day}{RESET}");
46-
eprintln!(" Missing input!");
47-
eprintln!(" Place input file in {BOLD}{WHITE}{path}{RESET}");
48-
49-
(stars, duration)
50-
}
51-
});
38+
.fold((0, Duration::ZERO), run_solution);
5239

5340
// Optionally print totals.
5441
if args().any(|arg| arg == "--totals") {
@@ -57,10 +44,27 @@ fn main() {
5744
}
5845
}
5946

60-
struct Solution {
61-
year: u32,
62-
day: u32,
63-
wrapper: fn(&str) -> (String, String),
47+
fn run_solution((stars, duration): (u32, Duration), solution: &Solution) -> (u32, Duration) {
48+
let Solution { year, day, wrapper } = solution;
49+
let path = format!("input/year{year}/day{day:02}.txt");
50+
51+
if let Ok(data) = read_to_string(&path) {
52+
let instant = Instant::now();
53+
let (part1, part2) = wrapper(&data);
54+
let elapsed = instant.elapsed();
55+
56+
println!("{BOLD}{YELLOW}{year} Day {day}{RESET}");
57+
println!(" Part 1: {part1}");
58+
println!(" Part 2: {part2}");
59+
60+
(stars + 2, duration + elapsed)
61+
} else {
62+
eprintln!("{BOLD}{RED}{year} Day {day}{RESET}");
63+
eprintln!(" Missing input!");
64+
eprintln!(" Place input file in {BOLD}{WHITE}{path}{RESET}");
65+
66+
(stars, duration)
67+
}
6468
}
6569

6670
macro_rules! run {

0 commit comments

Comments
 (0)