-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
152 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use crate::utils::day_number::DayNumber; | ||
use crate::utils::file_system::{read_input, write_input}; | ||
use crate::utils::year::Year; | ||
use aoc_client::AocClient; | ||
|
||
pub fn download_input(day_number: DayNumber, year: Year) { | ||
let input = read_input(day_number.to_string().as_str(), year.clone()); | ||
|
||
match input { | ||
Ok(_) => println!("Input already exists."), | ||
Err(_) => { | ||
println!("Downloading..."); | ||
let session = std::env::var("SESSION_COOKIE_ENV_VAR").unwrap(); | ||
|
||
let client = AocClient::builder() | ||
.session_cookie(session) | ||
.unwrap() | ||
.year(year.clone() as i32) | ||
.unwrap() | ||
.day(u32::from(day_number)) | ||
.unwrap() | ||
.build() | ||
.unwrap(); | ||
|
||
let input = client.get_input().unwrap(); | ||
|
||
write_input(&day_number.to_string(), year.clone(), &input).unwrap(); | ||
|
||
println!("Input downloaded"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod input; | ||
pub mod solve; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
use crate::solutions::solution; | ||
use crate::utils::day_number::DayNumber; | ||
use crate::utils::file_system::{read_input, read_output}; | ||
use crate::utils::year::Year; | ||
use std::fmt::{Display, Formatter}; | ||
use std::time::{Duration, Instant}; | ||
|
||
pub fn solve(day_number: &DayNumber, year: Year) { | ||
let solution = solution(*day_number, year.clone()); | ||
|
||
let input = match read_input(day_number.to_string().as_str(), year.clone()) { | ||
Ok(val) => val, | ||
Err(_) => panic!("Failed to read input. Download it first."), // todo better handle errors | ||
}; | ||
|
||
let output = read_output(day_number.to_string().as_str(), year); | ||
|
||
let expected: Vec<String> = output | ||
.unwrap_or(String::from("")) | ||
.lines() | ||
.map(|s| s.to_string()) | ||
.collect(); | ||
|
||
let expected_part_one = expected.first(); | ||
let expected_part_two = expected.get(1); | ||
|
||
println!( | ||
"{}", | ||
run("one", &|| solution.part_one(&input), expected_part_one) | ||
); | ||
println!( | ||
"{}", | ||
run("two", &|| solution.part_two(&input), expected_part_two) | ||
); | ||
} | ||
|
||
fn run<'a>( | ||
part: &str, | ||
solve_fn: &'a dyn Fn() -> String, | ||
expected: Option<&'a String>, | ||
) -> SolutionResult<'a> { | ||
let start = Instant::now(); | ||
let current: String = solve_fn(); | ||
let elapsed = start.elapsed(); | ||
|
||
SolutionResult { | ||
part: part.to_string(), | ||
expected, | ||
current, | ||
elapsed, | ||
} | ||
} | ||
|
||
struct SolutionResult<'a> { | ||
part: String, | ||
expected: Option<&'a String>, | ||
current: String, | ||
elapsed: Duration, | ||
} | ||
|
||
impl Display for SolutionResult<'_> { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
let result = match self.expected { | ||
None => "❔", | ||
Some(value) => { | ||
if value == &self.current { | ||
"✅" | ||
} else { | ||
"❌" | ||
} | ||
} | ||
}; | ||
let elapsed_in_ms = self.elapsed.as_nanos() as f64 / 1000.0 / 1000.0; | ||
|
||
write!( | ||
f, | ||
"Part {}: {} ({:.3}ms) {}", | ||
self.part, self.current, elapsed_in_ms, result | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.