-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
102 lines (86 loc) · 2.27 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// generate file that dynamically instantiates all solutions
use std::{
error::Error,
fs::{read_dir, File},
io,
io::Write,
path::{Path, PathBuf},
str,
};
fn days(input_dir: &str) -> io::Result<Vec<u32>> {
Ok(read_dir(input_dir)?
.flatten()
.filter(|e| e.path().is_file())
.flat_map(|e| e.file_name().into_string())
.flat_map(|s| {
str::from_utf8(&s.into_bytes()[3..])
.ok()
.and_then(|v| v.parse::<u32>().ok())
})
.collect())
}
fn gen_solutions_mod<P: AsRef<Path>>(p: P, days: &[u32]) -> io::Result<()> {
let mut f = File::create(p)?;
writeln!(f, "// DO NOT EDIT THIS FILE")?;
writeln!(f, "use crate::solver::Solver;")?;
writeln!(f)?;
for day in days {
writeln!(f, "mod day{0:02};", day)?;
}
writeln!(f)?;
writeln!(
f,
"pub fn exec_day(day: i32) {{
match day {{"
)?;
for day in days {
writeln!(f, " {0} => day{0:02}::Problem {{}}.solve(day),", day)?;
}
writeln!(
f,
" d => println!(\"Day {{}} hasn't been solved yet :(\", d),
}}
}}"
)?;
Ok(())
}
fn gen_solutions(dir: &str, days: &[u32]) -> io::Result<()> {
for day in days {
let file = PathBuf::from(format!("{}/day{:02}.rs", dir, day));
if file.exists() {
continue;
}
let mut f = File::create(file)?;
writeln!(
f,
"use crate::solver::Solver;
use std::io::Read;
pub struct Problem;
impl Solver for Problem {{
type Input = ();
type Output1 = u64;
type Output2 = u64;
fn parse_input<R: Read>(&self, r: R) -> Self::Input {{}}
fn solve_first(&self, input: &Self::Input) -> Self::Output1 {{
0
}}
fn solve_second(&self, input: &Self::Input) -> Self::Output2 {{
0
}}
}}
"
)?;
}
Ok(())
}
fn main() -> Result<(), Box<dyn Error>> {
let input_dir = "./input";
let output_dir = "./src/solutions";
let solutions_mod_output_path = Path::new(&output_dir).join("mod.rs");
let days = days(input_dir)?;
// write solutions mod file
gen_solutions_mod(&solutions_mod_output_path, &days)?;
// write solutions
gen_solutions(output_dir, &days)?;
Ok(())
}