Skip to content

Commit 8d09ec4

Browse files
committed
refacotr: cleanup main
1 parent e6c9c39 commit 8d09ec4

File tree

22 files changed

+152
-139
lines changed

22 files changed

+152
-139
lines changed

src/commands/input.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use crate::utils::day_number::DayNumber;
2+
use crate::utils::file_system::{read_input, write_input};
3+
use crate::utils::year::Year;
4+
use aoc_client::AocClient;
5+
6+
pub fn download_input(day_number: DayNumber, year: Year) {
7+
let input = read_input(day_number.to_string().as_str(), year.clone());
8+
9+
match input {
10+
Ok(_) => println!("Input already exists."),
11+
Err(_) => {
12+
println!("Downloading...");
13+
let session = std::env::var("SESSION_COOKIE_ENV_VAR").unwrap();
14+
15+
let client = AocClient::builder()
16+
.session_cookie(session)
17+
.unwrap()
18+
.year(year.clone() as i32)
19+
.unwrap()
20+
.day(u32::from(day_number))
21+
.unwrap()
22+
.build()
23+
.unwrap();
24+
25+
let input = client.get_input().unwrap();
26+
27+
write_input(&day_number.to_string(), year.clone(), &input).unwrap();
28+
29+
println!("Input downloaded");
30+
}
31+
}
32+
}

src/commands/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod input;
2+
pub mod solve;

src/commands/solve.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
use crate::solutions::solution;
2+
use crate::utils::day_number::DayNumber;
3+
use crate::utils::file_system::{read_input, read_output};
4+
use crate::utils::year::Year;
5+
use std::fmt::{Display, Formatter};
6+
use std::time::{Duration, Instant};
7+
8+
pub fn solve(day_number: &DayNumber, year: Year) {
9+
let solution = solution(*day_number, year.clone());
10+
11+
let input = match read_input(day_number.to_string().as_str(), year.clone()) {
12+
Ok(val) => val,
13+
Err(_) => panic!("Failed to read input. Download it first."), // todo better handle errors
14+
};
15+
16+
let output = read_output(day_number.to_string().as_str(), year);
17+
18+
let expected: Vec<String> = output
19+
.unwrap_or(String::from(""))
20+
.lines()
21+
.map(|s| s.to_string())
22+
.collect();
23+
24+
let expected_part_one = expected.first();
25+
let expected_part_two = expected.get(1);
26+
27+
println!(
28+
"{}",
29+
run("one", &|| solution.part_one(&input), expected_part_one)
30+
);
31+
println!(
32+
"{}",
33+
run("two", &|| solution.part_two(&input), expected_part_two)
34+
);
35+
}
36+
37+
fn run<'a>(
38+
part: &str,
39+
solve_fn: &'a dyn Fn() -> String,
40+
expected: Option<&'a String>,
41+
) -> SolutionResult<'a> {
42+
let start = Instant::now();
43+
let current: String = solve_fn();
44+
let elapsed = start.elapsed();
45+
46+
SolutionResult {
47+
part: part.to_string(),
48+
expected,
49+
current,
50+
elapsed,
51+
}
52+
}
53+
54+
struct SolutionResult<'a> {
55+
part: String,
56+
expected: Option<&'a String>,
57+
current: String,
58+
elapsed: Duration,
59+
}
60+
61+
impl Display for SolutionResult<'_> {
62+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
63+
let result = match self.expected {
64+
None => "❔",
65+
Some(value) => {
66+
if value == &self.current {
67+
"✅"
68+
} else {
69+
"❌"
70+
}
71+
}
72+
};
73+
let elapsed_in_ms = self.elapsed.as_nanos() as f64 / 1000.0 / 1000.0;
74+
75+
write!(
76+
f,
77+
"Part {}: {} ({:.3}ms) {}",
78+
self.part, self.current, elapsed_in_ms, result
79+
)
80+
}
81+
}

src/main.rs

Lines changed: 5 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
use crate::solutions::solution;
1+
use crate::commands::input::download_input;
2+
use crate::commands::solve::solve;
23
use crate::utils::year::Year;
34
use clap::{Parser, Subcommand};
4-
use std::fmt::{Display, Formatter};
5-
use std::time::{Duration, Instant};
6-
use aoc_client::{AocClient};
75
use dotenv::dotenv;
86
use utils::day_number::DayNumber;
9-
use utils::file_system::{read_input, read_output};
107
use utils::year::Year::Year2023;
11-
use crate::utils::file_system::write_input;
128

9+
mod commands;
1310
mod solutions;
1411
mod utils;
1512

1613
#[derive(Parser, Debug)]
1714
#[clap(author, version, about, long_about = None)]
18-
struct Args {
15+
struct Cli {
1916
#[command(subcommand)]
2017
command: Option<Command>,
2118
#[clap(short, long)]
@@ -46,7 +43,7 @@ fn parse_day(s: &str) -> Result<u8, String> {
4643
fn main() {
4744
dotenv().ok();
4845

49-
let cli = Args::parse();
46+
let cli = Cli::parse();
5047
let command = cli.command.unwrap_or(Command::Solve);
5148
let day = cli.day.unwrap_or(1);
5249
let day_number: DayNumber = DayNumber::try_from(day.to_string()).unwrap();
@@ -60,102 +57,3 @@ fn main() {
6057
Command::Input => download_input(day_number, year),
6158
}
6259
}
63-
64-
fn solve(day_number: &DayNumber, year: Year) {
65-
let solution = solution(&day_number, year.clone());
66-
67-
let input = match read_input(day_number.to_string().as_str(), year.clone()) {
68-
Ok(val) => val,
69-
Err(_) => panic!("Failed to read input. Download it first."), // todo better handle errors
70-
};
71-
72-
let output = read_output(day_number.to_string().as_str(), year);
73-
74-
let expected: Vec<String> = output
75-
.unwrap_or(String::from(""))
76-
.lines()
77-
.map(|s| s.to_string())
78-
.collect();
79-
80-
let expected_part_one = expected.first();
81-
let expected_part_two = expected.get(1);
82-
83-
println!(
84-
"{}",
85-
run("one", &|| solution.part_one(&input), expected_part_one)
86-
);
87-
println!(
88-
"{}",
89-
run("two", &|| solution.part_two(&input), expected_part_two)
90-
);
91-
}
92-
93-
fn download_input(day_number: DayNumber, year: Year) {
94-
let input = read_input(day_number.to_string().as_str(), year.clone());
95-
96-
match input {
97-
Ok(_) => println!("Input already exists."),
98-
Err(_) => {
99-
println!("Downloading...");
100-
let session = std::env::var("SESSION_COOKIE_ENV_VAR").unwrap();
101-
102-
let client = AocClient::builder()
103-
.session_cookie(session).unwrap()
104-
.year(year.clone() as i32).unwrap()
105-
.day(u32::from(day_number)).unwrap()
106-
.build().unwrap();
107-
108-
let input = client.get_input().unwrap();
109-
110-
write_input(&day_number.to_string(), year.clone(), &input).unwrap();
111-
112-
println!("Input downloaded");
113-
}
114-
}
115-
}
116-
117-
fn run<'a>(
118-
part: &str,
119-
solve_fn: &'a dyn Fn() -> String,
120-
expected: Option<&'a String>,
121-
) -> SolutionResult<'a> {
122-
let start = Instant::now();
123-
let current: String = solve_fn();
124-
let elapsed = start.elapsed();
125-
126-
SolutionResult {
127-
part: part.to_string(),
128-
expected,
129-
current,
130-
elapsed,
131-
}
132-
}
133-
134-
struct SolutionResult<'a> {
135-
part: String,
136-
expected: Option<&'a String>,
137-
current: String,
138-
elapsed: Duration,
139-
}
140-
141-
impl Display for SolutionResult<'_> {
142-
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
143-
let result = match self.expected {
144-
None => "❔",
145-
Some(value) => {
146-
if value == &self.current {
147-
"✅"
148-
} else {
149-
"❌"
150-
}
151-
}
152-
};
153-
let elapsed_in_ms = self.elapsed.as_nanos() as f64 / 1000.0 / 1000.0;
154-
155-
write!(
156-
f,
157-
"Part {}: {} ({:.3}ms) {}",
158-
self.part, self.current, elapsed_in_ms, result
159-
)
160-
}
161-
}

src/solutions/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use crate::utils::day_number::DayNumber;
21
use crate::solutions::year2023::day19;
2+
use crate::utils::day_number::DayNumber;
33
use crate::utils::year::Year;
44
use year2023::{
55
day01, day02, day03, day04, day05, day06, day07, day08, day09, day10, day11, day12, day13,
@@ -13,8 +13,8 @@ pub trait Solution {
1313
fn part_two(&self, input: &str) -> String;
1414
}
1515

16-
pub fn solution(day: &DayNumber, year: Year) -> Box<dyn Solution> {
17-
let i: u8 = (*day).into();
16+
pub fn solution(day: DayNumber, year: Year) -> Box<dyn Solution> {
17+
let i: u8 = day.into();
1818

1919
match year {
2020
Year::Year2023 => match i {
@@ -45,6 +45,6 @@ pub fn solution(day: &DayNumber, year: Year) -> Box<dyn Solution> {
4545
25 => Box::new(day25::Day25),
4646
_ => panic!("Day not exist"),
4747
},
48-
Year::Year2024 => todo!("2024 is not implemented yet")
48+
Year::Year2024 => todo!("2024 is not implemented yet"),
4949
}
5050
}

src/solutions/year2023/day05.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use crate::utils::range::Range;
21
use crate::solutions::Solution;
2+
use crate::utils::range::Range;
33
use std::collections::HashMap;
44
use std::str;
55

@@ -189,10 +189,10 @@ impl MapRange {
189189

190190
#[cfg(test)]
191191
mod tests {
192-
use crate::utils::range::Range;
193192
use crate::solutions::year2023::day05::{parse_input_part_one, Day05, Map, MapRange};
194193
use crate::solutions::year2023::read_2023_example;
195194
use crate::solutions::Solution;
195+
use crate::utils::range::Range;
196196
use std::vec;
197197

198198
#[test]

src/solutions/year2023/day08.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
use crate::solutions::Solution;
12
use crate::utils::chain_pattern_finder::Chain;
23
use crate::utils::math::lcm;
3-
use crate::solutions::Solution;
44
use regex::Regex;
55
use std::collections::hash_map::Entry::Vacant;
66
use std::collections::HashMap;

src/solutions/year2023/day10.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
use crate::solutions::Solution;
12
use crate::utils::direction::Direction;
23
use crate::utils::grid::Grid;
34
use crate::utils::point::Point;
45
use crate::utils::shoelace_formula::shoelace_formula;
5-
use crate::solutions::Solution;
66
use std::fmt;
77
use std::fmt::Display;
88
use std::ops::{Div, Sub};
@@ -157,10 +157,10 @@ impl Display for Tile {
157157

158158
#[cfg(test)]
159159
mod tests {
160-
use crate::utils::point::Point;
161160
use crate::solutions::year2023::day10::{Day10, Tile};
162161
use crate::solutions::year2023::read_2023_example;
163162
use crate::solutions::Solution;
163+
use crate::utils::point::Point;
164164

165165
#[test]
166166
fn part_one_example_test() {

src/solutions/year2023/day11.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
use crate::solutions::Solution;
12
use crate::utils::grid::Grid;
23
use crate::utils::pair_generator::pairs;
34
use crate::utils::point::Point;
4-
use crate::solutions::Solution;
55
use std::cmp::{max, min};
66
use std::collections::BTreeMap;
77
use std::ops::Mul;

src/solutions/year2023/day13.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
use crate::solutions::Solution;
12
use crate::utils::grid::Grid;
23
use crate::utils::point::Point;
34
use crate::utils::range::Range;
4-
use crate::solutions::Solution;
55
use std::collections::BTreeMap;
66
use std::fmt::{Display, Formatter};
77

@@ -171,10 +171,10 @@ impl Display for Type {
171171

172172
#[cfg(test)]
173173
mod tests {
174-
use crate::utils::grid::Grid;
175174
use crate::solutions::year2023::day13::{Day13, Type};
176175
use crate::solutions::year2023::read_2023_example;
177176
use crate::solutions::Solution;
177+
use crate::utils::grid::Grid;
178178

179179
#[test]
180180
fn part_one_example_test() {

0 commit comments

Comments
 (0)