3 Commits

Author SHA1 Message Date
2c64028978 Enable debug information for release 2021-11-28 17:13:27 +01:00
89159137fe Add other days 2021-11-28 17:12:26 +01:00
186d91d1b7 Use function pointers over dyn traits 2021-11-28 16:49:37 +01:00
28 changed files with 308 additions and 37 deletions

View File

@@ -6,3 +6,7 @@ edition = "2021"
[dependencies]
clap = "3.0.0-beta.5"
[profile.release]
# Keep debug information in release for better flamegraphs
debug = true

View File

@@ -1,10 +1,9 @@
use crate::Solution;
use std::io::Read;
#[derive(Default)]
pub struct Day01;
impl Solution for Day01 {
fn part1(&mut self, _input: &mut dyn std::io::Read) -> String {
pub fn part1(_input: &mut dyn Read) -> String {
todo!()
}
pub fn part2(_input: &mut dyn Read) -> String {
todo!()
}

9
2021/src/day02.rs Normal file
View File

@@ -0,0 +1,9 @@
use std::io::Read;
pub fn part1(_input: &mut dyn Read) -> String {
todo!()
}
pub fn part2(_input: &mut dyn Read) -> String {
todo!()
}

9
2021/src/day03.rs Normal file
View File

@@ -0,0 +1,9 @@
use std::io::Read;
pub fn part1(_input: &mut dyn Read) -> String {
todo!()
}
pub fn part2(_input: &mut dyn Read) -> String {
todo!()
}

9
2021/src/day04.rs Normal file
View File

@@ -0,0 +1,9 @@
use std::io::Read;
pub fn part1(_input: &mut dyn Read) -> String {
todo!()
}
pub fn part2(_input: &mut dyn Read) -> String {
todo!()
}

9
2021/src/day05.rs Normal file
View File

@@ -0,0 +1,9 @@
use std::io::Read;
pub fn part1(_input: &mut dyn Read) -> String {
todo!()
}
pub fn part2(_input: &mut dyn Read) -> String {
todo!()
}

9
2021/src/day06.rs Normal file
View File

@@ -0,0 +1,9 @@
use std::io::Read;
pub fn part1(_input: &mut dyn Read) -> String {
todo!()
}
pub fn part2(_input: &mut dyn Read) -> String {
todo!()
}

9
2021/src/day07.rs Normal file
View File

@@ -0,0 +1,9 @@
use std::io::Read;
pub fn part1(_input: &mut dyn Read) -> String {
todo!()
}
pub fn part2(_input: &mut dyn Read) -> String {
todo!()
}

9
2021/src/day08.rs Normal file
View File

@@ -0,0 +1,9 @@
use std::io::Read;
pub fn part1(_input: &mut dyn Read) -> String {
todo!()
}
pub fn part2(_input: &mut dyn Read) -> String {
todo!()
}

9
2021/src/day09.rs Normal file
View File

@@ -0,0 +1,9 @@
use std::io::Read;
pub fn part1(_input: &mut dyn Read) -> String {
todo!()
}
pub fn part2(_input: &mut dyn Read) -> String {
todo!()
}

9
2021/src/day10.rs Normal file
View File

@@ -0,0 +1,9 @@
use std::io::Read;
pub fn part1(_input: &mut dyn Read) -> String {
todo!()
}
pub fn part2(_input: &mut dyn Read) -> String {
todo!()
}

9
2021/src/day11.rs Normal file
View File

@@ -0,0 +1,9 @@
use std::io::Read;
pub fn part1(_input: &mut dyn Read) -> String {
todo!()
}
pub fn part2(_input: &mut dyn Read) -> String {
todo!()
}

9
2021/src/day12.rs Normal file
View File

@@ -0,0 +1,9 @@
use std::io::Read;
pub fn part1(_input: &mut dyn Read) -> String {
todo!()
}
pub fn part2(_input: &mut dyn Read) -> String {
todo!()
}

9
2021/src/day13.rs Normal file
View File

@@ -0,0 +1,9 @@
use std::io::Read;
pub fn part1(_input: &mut dyn Read) -> String {
todo!()
}
pub fn part2(_input: &mut dyn Read) -> String {
todo!()
}

9
2021/src/day14.rs Normal file
View File

@@ -0,0 +1,9 @@
use std::io::Read;
pub fn part1(_input: &mut dyn Read) -> String {
todo!()
}
pub fn part2(_input: &mut dyn Read) -> String {
todo!()
}

9
2021/src/day15.rs Normal file
View File

@@ -0,0 +1,9 @@
use std::io::Read;
pub fn part1(_input: &mut dyn Read) -> String {
todo!()
}
pub fn part2(_input: &mut dyn Read) -> String {
todo!()
}

9
2021/src/day16.rs Normal file
View File

@@ -0,0 +1,9 @@
use std::io::Read;
pub fn part1(_input: &mut dyn Read) -> String {
todo!()
}
pub fn part2(_input: &mut dyn Read) -> String {
todo!()
}

9
2021/src/day17.rs Normal file
View File

@@ -0,0 +1,9 @@
use std::io::Read;
pub fn part1(_input: &mut dyn Read) -> String {
todo!()
}
pub fn part2(_input: &mut dyn Read) -> String {
todo!()
}

9
2021/src/day18.rs Normal file
View File

@@ -0,0 +1,9 @@
use std::io::Read;
pub fn part1(_input: &mut dyn Read) -> String {
todo!()
}
pub fn part2(_input: &mut dyn Read) -> String {
todo!()
}

9
2021/src/day19.rs Normal file
View File

@@ -0,0 +1,9 @@
use std::io::Read;
pub fn part1(_input: &mut dyn Read) -> String {
todo!()
}
pub fn part2(_input: &mut dyn Read) -> String {
todo!()
}

9
2021/src/day20.rs Normal file
View File

@@ -0,0 +1,9 @@
use std::io::Read;
pub fn part1(_input: &mut dyn Read) -> String {
todo!()
}
pub fn part2(_input: &mut dyn Read) -> String {
todo!()
}

9
2021/src/day21.rs Normal file
View File

@@ -0,0 +1,9 @@
use std::io::Read;
pub fn part1(_input: &mut dyn Read) -> String {
todo!()
}
pub fn part2(_input: &mut dyn Read) -> String {
todo!()
}

9
2021/src/day22.rs Normal file
View File

@@ -0,0 +1,9 @@
use std::io::Read;
pub fn part1(_input: &mut dyn Read) -> String {
todo!()
}
pub fn part2(_input: &mut dyn Read) -> String {
todo!()
}

9
2021/src/day23.rs Normal file
View File

@@ -0,0 +1,9 @@
use std::io::Read;
pub fn part1(_input: &mut dyn Read) -> String {
todo!()
}
pub fn part2(_input: &mut dyn Read) -> String {
todo!()
}

9
2021/src/day24.rs Normal file
View File

@@ -0,0 +1,9 @@
use std::io::Read;
pub fn part1(_input: &mut dyn Read) -> String {
todo!()
}
pub fn part2(_input: &mut dyn Read) -> String {
todo!()
}

5
2021/src/day25.rs Normal file
View File

@@ -0,0 +1,5 @@
use std::io::Read;
pub fn part1(_input: &mut dyn Read) -> String {
todo!()
}

View File

@@ -1,29 +1,90 @@
use std::io::Read;
type Solution = fn(&mut dyn Read) -> String;
mod day01;
mod day02;
mod day03;
mod day04;
mod day05;
mod day06;
mod day07;
mod day08;
mod day09;
mod day10;
mod day11;
mod day12;
mod day13;
mod day14;
mod day15;
mod day16;
mod day17;
mod day18;
mod day19;
mod day20;
mod day21;
mod day22;
mod day23;
mod day24;
mod day25;
pub trait Solution {
fn part1(&mut self, input: &mut dyn Read) -> String;
fn part2(&mut self, _input: &mut dyn Read) -> String {
unimplemented!("Still working on part 1");
}
}
pub fn get_implementation(day: usize) -> Box<dyn Solution> {
pub fn get_implementation(day: usize, part2: bool) -> Solution {
if !part2 {
match day {
1 => Box::new(day01::Day01::default()),
_ => panic!("Unsupported day {}", day),
1 => day01::part1,
2 => day02::part1,
3 => day03::part1,
4 => day04::part1,
5 => day05::part1,
6 => day06::part1,
7 => day07::part1,
8 => day08::part1,
9 => day09::part1,
10 => day10::part1,
11 => day11::part1,
12 => day12::part1,
13 => day13::part1,
14 => day14::part1,
15 => day15::part1,
16 => day16::part1,
17 => day17::part1,
18 => day18::part1,
19 => day19::part1,
20 => day20::part1,
21 => day21::part1,
22 => day22::part1,
23 => day23::part1,
24 => day24::part1,
25 => day25::part1,
_ => panic!("Unsupported part one for day {}", day),
}
} else {
match day {
1 => day01::part2,
2 => day02::part2,
3 => day03::part2,
4 => day04::part2,
5 => day05::part2,
6 => day06::part2,
7 => day07::part2,
8 => day08::part2,
9 => day09::part2,
10 => day10::part2,
11 => day11::part2,
12 => day12::part2,
13 => day13::part2,
14 => day14::part2,
15 => day15::part2,
16 => day16::part2,
17 => day17::part2,
18 => day18::part2,
19 => day19::part2,
20 => day20::part2,
21 => day21::part2,
22 => day22::part2,
23 => day23::part2,
24 => day24::part2,
_ => panic!("Unsupported part two for day {}", day),
}
}
#[cfg(test)]
fn test_implementation(mut day: impl Solution, part: u8, mut input: &[u8], answer: impl ToString) {
let result = match part {
1 => day.part1(&mut input),
2 => day.part2(&mut input),
_ => panic!("Invalid part: {}", part),
};
assert_eq!(answer.to_string(), result);
}

View File

@@ -30,7 +30,6 @@ struct Opts {
fn main() {
let opts: Opts = Opts::parse();
let mut implementation = get_implementation(opts.day.get());
let mut input: Box<dyn Read> = if let Some(input) = opts.input {
Box::new(File::open(&input).expect("Failed to open input"))
} else {
@@ -38,11 +37,7 @@ fn main() {
};
let begin = Instant::now();
let result = if opts.part2 {
implementation.part2(&mut input)
} else {
implementation.part1(&mut input)
};
let result = get_implementation(opts.day.get(), opts.part2)(&mut *input);
if opts.time {
eprintln!("Execution time: {:?}", Instant::now().duration_since(begin));