-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.ps1
169 lines (143 loc) · 5.24 KB
/
create.ps1
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[ValidateRange(0, 25)]
[Alias('d')]
[int]
$day
)
# If the file already exists, don't write to it -- especially since we might have
# made changes to the already existing file.
$new_prob_file_name = [string]::Format("src/aoc/day{0:d2}.rs", $day)
if (Test-Path -Path $new_prob_file_name) {
Write-Warning ([string]::Format("File 'day{0:d2}.rs' already exists", $day))
exit 1
}
# =================================================================================== #
# Create the problem file #
# =================================================================================== #
$base_problem_str = @'
use crate::aoc::aoc_problem::{{AoCProblem, Solution}};
pub struct Day{0:d2} {{
// fields here
}}
impl AoCProblem for Day{0:d2} {{
fn prepare(input: &str) -> Self {{
Self {{}}
}}
fn part1(&mut self) -> Solution {{
0.into()
}}
fn part2(&mut self) -> Solution {{
0.into()
}}
}}
'@
[string]::Format($base_problem_str, $day) | Out-File -FilePath $new_prob_file_name -Encoding UTF8
if (!$?) {
Write-Warning ([string]::Format("Failed to write to file 'day{0:d2}.rs'", $day))
exit 1
}
# =================================================================================== #
# Update the mod.rs file #
# =================================================================================== #
$mod_append_str = @'
mod day{0:d2};
pub use day{0:d2}::Day{0:d2};
'@
Add-Content -Path "src/aoc/mod.rs" -Value ([string]::Format($mod_append_str, $day))
if (!$?) {
Write-Warning ([string]::Format("Failed to append to file 'mod.rs'"))
exit 1
}
# =================================================================================== #
# Create the input files #
# =================================================================================== #
$input_name = [string]::Format("input/day{0:d2}.txt", $day)
if (!(Test-Path -Path $input_name)) {
New-Item -Path $input_name -ItemType File | Out-Null
if (!$?) {
Write-Warning ([string]::Format("Failed to create file 'day{0:d2}.txt'", $day))
exit 1
}
}
# =================================================================================== #
# Finally, update run.rs #
# =================================================================================== #
$run_enum_base = ""
# Go through each file in the aoc directory
$files = Get-ChildItem -Path "src/aoc" -Filter "*.rs"
foreach ($file in $files) {
# Does it have a number before the .rs?
if ($file.Name -match "day(\d{2})\.rs") {
# Get the number
$num = [int]::Parse($Matches[1])
$run_enum_base += [string]::Format(" {0} => Box::new(aoc::Day{0:d2}::prepare(&input_str)),`n", $num)
}
}
# Finally, apply $run_enum_base to the file base
$run_base = @'
use std::{{fs, path::Path, time::Instant}};
use crate::*;
use crate::aoc::{{self, AoCProblem}};
/// Runs your solution to specified day.
///
/// # Parameters
/// - `day`: The day to run. This should be in the range [0, 25].
/// - `test_case`: The test case to run, if any. If `None`, then the
/// solution file is executed.
///
/// # Returns
/// A result representing whether the execution was successful or not.
pub fn run(day: u32, test_case: Option<u32>) -> RunResult {{
// Look for input file.
let input_file = Path::new("input").join(if let Some(t) = test_case {{
format!("day{{:02}}_test{{}}.txt", day, t)
}} else {{
format!("day{{:02}}.txt", day)
}});
if !input_file.exists() {{
return RunResult::InputFileNotFound(input_file);
}}
let mut start = Instant::now();
let input_str = match fs::read_to_string(&input_file) {{
Ok(o) => o,
Err(_) => return RunResult::InputFileNotValid(input_file),
}};
let mut solver: Box<dyn AoCProblem> = match day {{
{0}
_ => return RunResult::ProblemNotFound(day),
}};
let input_time = start.elapsed();
// Part 1
start = Instant::now();
println!("Part 1 Solution: {{}}", solver.part1());
let p1_t = start.elapsed();
// Part 2
start = Instant::now();
println!("Part 2 Solution: {{}}", solver.part2());
let p2_t = start.elapsed();
// Execution ends, display time statistics.
println!();
match test_case {{
Some(t) => println!("[!] Running Code for Test Case {{}}.", t),
None => println!("[.] Running Code for Solution."),
}}
println!("Input Parse : \t{{}} ms.", input_time.as_millis());
println!("Part 1 Time : \t{{}} ms.", p1_t.as_millis());
println!("Part 2 Time : \t{{}} ms.", p2_t.as_millis());
println!();
println!("P1 + P2 : \t{{}} ms.", (p1_t + p2_t).as_millis(),);
println!(
"P + P1 + P2 : \t{{}} ms.",
(input_time + p1_t + p2_t).as_millis(),
);
RunResult::Success
}}
'@
[string]::Format($run_base, $run_enum_base.Trim()) | Out-File -FilePath "src/run.rs" -Encoding UTF8
if (!$?) {
Write-Warning "Unable to apply changes to 'run.rs'; please do so manually."
exit 1
}
exit 0