Skip to content

Commit ca75bce

Browse files
committed
helpers refactor
1 parent 9e2bfc9 commit ca75bce

File tree

6 files changed

+50
-49
lines changed

6 files changed

+50
-49
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ My attempt to solve the [AoC2022](https://adventofcode.com/2022/) with the [Rust
1717

1818
### Usage
1919

20-
- Download your puzzles inputs and put them into the `data/inputs` folder. Name them `input01.txt`, `input02.txt`, etc
20+
- Download your the inputs for each puzzle and put them into the `data/inputs` folder. Name them `input01.txt`, `input02.txt`, etc
2121
- To run all solutions, run `cargo run --release`
2222
- To run the solutions to days `X`, `Y` and `Z`, run `cargo run --release -- X Y Z`
2323

src/helpers.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//! Helpers for AoC. Includes macros, reading functions, etc
2+
3+
use {
4+
crate::solutions,
5+
std::fs::File,
6+
std::io::{self, BufRead},
7+
std::path::Path,
8+
};
9+
10+
/// solves and displays pt1 and pt2 of `day`
11+
pub fn solve_day(day: u8) {
12+
let filename = format!("./data/inputs/input{day:02}.txt");
13+
match day {
14+
1 => {
15+
let (answer1, time1) = solutions::day01::pt1(&filename);
16+
let (answer2, time2) = solutions::day01::pt2(&filename);
17+
18+
println!("Day {day:02}");
19+
println!(" part 1: {answer1}, elapsed time: {time1} ms");
20+
println!(" part 2: {answer2}, elapsed time: {time2} ms");
21+
}
22+
2 => {
23+
let (answer1, time1) = solutions::day02::pt1(&filename);
24+
let (answer2, time2) = solutions::day02::pt2(&filename);
25+
26+
println!("Day {day:02}");
27+
println!(" part 1: {answer1}, elapsed time: {time1} ms");
28+
println!(" part 2: {answer2}, elapsed time: {time2} ms");
29+
}
30+
_ => println!("Day {day:02}\n not implemented!"),
31+
}
32+
}
33+
34+
/// returns an iterator over each line in the input file
35+
pub fn read_lines<P>(filename: P) -> std::io::Lines<std::io::BufReader<std::fs::File>>
36+
where
37+
P: AsRef<Path>,
38+
{
39+
io::BufReader::new(File::open(filename).expect("not able to open file")).lines()
40+
}

src/main.rs

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
use {
2-
path::Path,
3-
std::fs::File,
4-
std::io::{self, BufRead},
5-
std::{env, path},
6-
};
1+
use std::env;
72

3+
mod helpers;
84
mod solutions;
95

106
const SOLVED: [u8; 2] = [1, 2];
@@ -16,47 +12,15 @@ fn main() {
1612
if args.len() == 1 {
1713
// if no arguments given, run all available solutions
1814
SOLVED.into_iter().for_each(|day| {
19-
solve_day(day);
15+
helpers::solve_day(day);
2016
})
2117
} else {
2218
// if arguments are given, run the respective solutions
2319
args.into_iter().skip(1).for_each(|arg| {
2420
let day = arg
2521
.parse()
26-
.expect("not able to parse command line argument {arg}");
27-
solve_day(day);
22+
.unwrap_or_else(|_| panic!("not able to parse command line argument {arg}"));
23+
helpers::solve_day(day);
2824
})
2925
}
3026
}
31-
32-
/// call functions to solve pt1 and pt2 of `day`
33-
fn solve_day(day: u8) {
34-
let filename = format!("./data/inputs/input{day:02}.txt");
35-
match day {
36-
1 => {
37-
let (answer1, time1) = solutions::day01::pt1(&filename);
38-
let (answer2, time2) = solutions::day01::pt2(&filename);
39-
40-
println!("Day {day:02}");
41-
println!(" part 1: {answer1}, elapsed time: {time1} ms");
42-
println!(" part 2: {answer2}, elapsed time: {time2} ms");
43-
}
44-
2 => {
45-
let (answer1, time1) = solutions::day02::pt1(&filename);
46-
let (answer2, time2) = solutions::day02::pt2(&filename);
47-
48-
println!("Day {day:02}");
49-
println!(" part 1: {answer1}, elapsed time: {time1} ms");
50-
println!(" part 2: {answer2}, elapsed time: {time2} ms");
51-
}
52-
_ => println!("Day {day:02}\n not implemented!"),
53-
}
54-
}
55-
56-
/// returns an iterator over each line in the input file
57-
fn read_lines<P>(filename: P) -> std::io::Lines<std::io::BufReader<std::fs::File>>
58-
where
59-
P: AsRef<Path>,
60-
{
61-
io::BufReader::new(File::open(filename).expect("not able to open file")).lines()
62-
}

src/solutions.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
//! Module that contains `day01`, `day02`... submodules, each with their solving functions
2+
13
pub mod day01;
24
pub mod day02;

src/solutions/day01.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@
44
//!
55
//! https://adventofcode.com/2022/day/1
66
7-
use std::time::SystemTime;
7+
use {crate::helpers::read_lines, std::time::SystemTime};
88

9-
use crate::read_lines;
10-
11-
// finds the block with the largest sum in the file
129
pub fn pt1(filename: &str) -> (u32, u32) {
1310
let time = SystemTime::now();
1411

src/solutions/day02.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
//!
55
//! https://adventofcode.com/2022/day/2
66
7-
use std::time::SystemTime;
8-
9-
use crate::read_lines;
7+
use {crate::helpers::read_lines, std::time::SystemTime};
108

119
pub fn pt1(filename: &str) -> (u32, u32) {
1210
let time = SystemTime::now();

0 commit comments

Comments
 (0)