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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//! Implementation details of the `bin_setup` procedural macro for my Advent of Code `aoc-core` library crate.
//!
//! You probably don't want to use this crate directly,
//! although it can be useful if you use your own library.
use TokenStream;
use quote;
use ;
use BinSetupInput;
/// This macro can be used to generate my standard approach to Advent of Code binaries.
///
/// It can only be attached to `fn main()`.\
/// Required arguments:
/// - `puzzles_count`, number of puzzles in the binary
/// - `resources_directory`, path to inputs and answers files, relative to `src`
/// - `input_extension`, input files extension
/// - `answers_file`, file containing the puzzle answers for checking execution
///
/// A simple `pretty_solution_2` macro is provided to:
/// - embed input strings
/// - execute solutions
/// - measure execution time
/// - verify output against provided answers
///
/// The generated main function provides `args` handling to execute only specific days, e.g. `$ executable 1 3 5`
///
/// Example usage:
/// ```
/// use aoc_core_macros::bin_setup;
///
/// #[bin_setup(5, "../resources", ".in", "Answers.out")]
/// fn main() {
/// pretty_solution_2!(5, "PuzzleX", solution1, solution2);
/// }
///
/// fn solution1(input: &str) -> u8 {
/// input.lines().map(|n| n.parse::<u8>().unwrap()).sum()
/// }
///
/// fn solution2(input: &str) -> u8 {
/// input.lines().map(|n| n.parse::<u8>().unwrap()).product()
/// }
///
/// // ../resources/PuzzleX.in
/// // 2
/// // 3
///
/// // ../resources/Answers.out
/// // PuzzleX 5 6
/// ```
///
/// Generated code looks like this:
/// ```
/// #[allow(clippy::items_after_statements)]
/// fn main() {
/// let puzzle_answers: rustc_hash::FxHashMap<&'static str, [&'static str; 2]> =
/// include_str!(concat!("../resources", "/", "Answers.out"))
/// .lines()
/// .map(|line| {
/// let parts: Vec<_> = line.split_ascii_whitespace().collect();
///
/// (parts[0], [parts[1], parts[2]])
/// })
/// .collect();
///
/// let selected_puzzles: [bool; 5] = {
/// let args: Vec<_> = std::env::args().collect();
///
/// if args.len() == 1 {
/// [true; 5]
/// } else {
/// std::array::from_fn(|day| args.contains(&(day + 1).to_string()))
/// }
/// };
///
/// #[inline]
/// fn pretty_solution<R>(
/// puzzle: &str,
/// part: usize,
/// solution: fn(&str) -> R,
/// input: &str,
/// answer: &str,
/// ) where
/// R: std::fmt::Display + PartialEq,
/// {
/// let now = std::time::Instant::now();
/// let solution = solution(input);
/// let microseconds = now.elapsed().as_micros();
///
/// assert!(
/// solution.to_string() == answer,
/// "Wrong solution for {puzzle} part {part}: expected {answer}, but got {solution}"
/// );
///
/// println!("{part} -> {answer} ({microseconds}μs)");
/// }
///
/// macro_rules! pretty_solution_2 {
/// ($day:literal, $puzzle: literal, $solution1:ident $(,$solution2:ident)?) => {
/// if selected_puzzles[$day - 1] {
/// println!("Day {}: {}", $day, $puzzle);
///
/// const INPUT: &str =
/// include_str!(concat!("../resources", "/", $puzzle, ".in"));
/// let answers = puzzle_answers.get($puzzle).expect("Puzzle answer not found");
///
/// pretty_solution($puzzle, 1, $solution1, INPUT, answers[0]);
///
/// $(pretty_solution($puzzle, 2, $solution2, INPUT, answers[1]);)?
///
/// println!();
/// }
/// };
/// }
///
/// pretty_solution_2!(5, "PuzzleX", solution1, solution2);
/// }
///
/// # fn solution1(input: &str) -> u8 {
/// # input.lines().map(|n| n.parse::<u8>().unwrap()).sum()
/// # }
/// #
/// # fn solution2(input: &str) -> u8 {
/// # input.lines().map(|n| n.parse::<u8>().unwrap()).product()
/// # }
/// ```