setup
This commit is contained in:
parent
83f51b4452
commit
3bff18f34f
7 changed files with 48 additions and 22 deletions
|
|
@ -1,5 +1,5 @@
|
|||
use shared::{Answer, Solution};
|
||||
use regex::{Regex};
|
||||
use regex::Regex;
|
||||
|
||||
pub struct Day01;
|
||||
|
||||
|
|
@ -22,7 +22,7 @@ impl Solution for Day01 {
|
|||
}
|
||||
}
|
||||
|
||||
sum += 10 * first.expect(word) + last.expect(word)
|
||||
sum += 10 * first.unwrap_or(0) + last.unwrap_or(0)
|
||||
}
|
||||
|
||||
Answer::from(sum)
|
||||
|
|
@ -40,23 +40,19 @@ impl Solution for Day01 {
|
|||
).unwrap();
|
||||
|
||||
Answer::from(words.iter()
|
||||
.map(|word| {10 * matches(&first.captures(&word).unwrap()) + matches(&last.captures(word).unwrap())})
|
||||
.map(|word| {10 * matches(first.captures(word)) + matches(last.captures(word))})
|
||||
.collect::<Vec<_>>()
|
||||
.iter()
|
||||
.sum::<u64>())
|
||||
}
|
||||
}
|
||||
|
||||
fn matches(cap: ®ex::Captures) -> u64 {
|
||||
fn matches(cap: Option<regex::Captures>) -> u64 {
|
||||
if cap.is_none() { return 0} ;
|
||||
|
||||
for i in 1..10 {
|
||||
if cap.get(i).is_some() { return i as u64 };
|
||||
if cap.as_ref().unwrap().get(i).is_some() { return i as u64 };
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn example() {
|
||||
assert_eq!(Day01.part_1(load_test(2023, 1)?.as_str()), 142);
|
||||
assert_eq!(Day01.part_2(load_test(2023, 1)?.as_str()), 281);
|
||||
0
|
||||
}
|
||||
|
||||
|
|
|
|||
6
examples/2024/day01
Normal file
6
examples/2024/day01
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3
|
||||
2
examples/2024/day01_solution
Normal file
2
examples/2024/day01_solution
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
11
|
||||
31
|
||||
6
examples/2024/day02
Normal file
6
examples/2024/day02
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
7 6 4 2 1
|
||||
1 2 7 8 9
|
||||
9 7 6 2 1
|
||||
1 3 2 4 5
|
||||
8 6 4 4 1
|
||||
1 3 6 7 9
|
||||
2
examples/2024/day02_solution
Normal file
2
examples/2024/day02_solution
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
2
|
||||
4
|
||||
|
|
@ -2,11 +2,14 @@ use std::{fs, io};
|
|||
|
||||
pub fn load_actual(year: usize, day: usize) -> io::Result<String> {
|
||||
let path = format!("./data/{year}/day{:02}", day);
|
||||
fs::read_to_string(path)
|
||||
let file = fs::read_to_string(path)?;
|
||||
Ok(file.trim().into())
|
||||
}
|
||||
|
||||
pub fn load_test(year: usize, day: usize) -> (io::Result<String>, io::Result<String>) {
|
||||
pub fn load_test(year: usize, day: usize) -> Result<(String, String), io::Error> {
|
||||
let input_path = format!("./examples/{year}/day{:02}", day);
|
||||
let result_path = format!("./examples/{year}/day{:02}_solution", day);
|
||||
(fs::read_to_string(input_path), fs::read_to_string(result_path))
|
||||
let out1 = fs::read_to_string(input_path)?;
|
||||
let out2 = fs::read_to_string(result_path)?;
|
||||
Ok((out1.trim().into(), out2.trim().into()))
|
||||
}
|
||||
|
|
|
|||
25
src/main.rs
25
src/main.rs
|
|
@ -10,21 +10,25 @@ use fetch_input::fetch_input;
|
|||
use std::{path::Path, time::Instant, env};
|
||||
|
||||
fn main() {
|
||||
run_daily();
|
||||
}
|
||||
|
||||
fn run_daily() {
|
||||
let year = env::var("ADVENT_YEAR")
|
||||
.unwrap_or("2024".into())
|
||||
.unwrap()
|
||||
.parse()
|
||||
.unwrap();
|
||||
let day = env::var("ADVENT_DAY")
|
||||
.unwrap_or("1".into())
|
||||
.unwrap()
|
||||
.parse()
|
||||
.unwrap();
|
||||
|
||||
if ! Path::new(&format!("./data/{}/day{:02}", year, day)).exists()
|
||||
&& env::var("ADVENT_TOKEN").is_ok() {
|
||||
fetch_input(year, day).expect("Set ADVENT_TOKEN to the correct session cookie to fetch input automatically");
|
||||
}
|
||||
fetch_input(year, day).expect("Token Failed");
|
||||
|
||||
run_all();
|
||||
let data = load_actual(year, day).unwrap();
|
||||
|
||||
println!("Solved 1: {}", solution(&data, year, day, 1).unwrap());
|
||||
println!("Solved 2: {}", solution(&data, year, day, 1).unwrap());
|
||||
}
|
||||
|
||||
fn run_all() {
|
||||
|
|
@ -38,8 +42,11 @@ fn run_all() {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for year in years_to_run {
|
||||
let mut total = 0;
|
||||
|
||||
for day in 1..=25 {
|
||||
for part in [1, 2] {
|
||||
let input = load_actual(year, day).unwrap_or_else(|_| panic!("Missing input for {}/{:02}", year, day));
|
||||
|
|
@ -47,8 +54,12 @@ fn run_all() {
|
|||
let now = Instant::now();
|
||||
let result = solution(&input, year, day, part).unwrap_or_else(|| panic!("No solution at {}/{:02}", year, day));
|
||||
let time_passed = now.elapsed().as_millis();
|
||||
|
||||
total += time_passed;
|
||||
|
||||
println!("Running Year {}, Day {:02}, Part {} - Result {} - took {}ms", year, day, part, result, time_passed);
|
||||
}
|
||||
}
|
||||
println!("\nTook a total of {total}ms to run {year}!");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue