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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
//! `lib_aoc` is a simple trait-based framework for the annual [Advent of Code](https://adventofcode.com/) programming challenge.
//!
//! Focus less on the boilerplate and more on the problem by automatically wiring up your solutions with input loading, pretty-printing
//! and granular benchmarking.
//!
//! ## Getting Started
//! Create a new binary crate and add `lib_aoc` as a dependency.
//! ``` shell
//! $ cargo new advent_of_code && cd advent_of_code
//! $ cargo add lib_aoc
//! ```
//! Then, import the `lib_aoc` prelude and create a new struct to link your solutions.
//! ``` ignore
//! use lib_aoc::prelude::*;
//!
//! // Can be named whatever you'd like.
//! struct Solutions {}
//!
//! fn main() { /* ... */ }
//! ```
//! When solving a problem, you'll implement the [`Solution`] trait on this struct, and `lib_aoc` will
//! take care of connecting everything together.
//!
//! Before you can do that, however, you'll need to implement the [`Solver`] trait on the struct, which
//! (among other, optional things) tells `lib_aoc` how you'd like puzzle inputs to be loaded.
//!
//! The simple approach is to just read the input from disk, but more complex approaches
//! (such as scraping the Advent of Code website directly) are certainly possible.
//! ``` ignore
//! impl Solver for Solutions {
//! fn load(day: u8) -> String {
//! std::fs::read_to_string(format!("src/inputs/{day:02}.txt"))
//! .expect("Puzzle input could not be read.")
//! }
//!
//! // Note that a test loading implementation can be elided if one is not desired;
//! // the default implementation will simply panic.
//! fn load_test(day: u8, part: bool) -> String {
//! std::fs::read_to_string(format!("src/inputs/test_{day:02}.txt"))
//! .expect("Puzzle input could not be read.")
//! }
//! }
//! ```
//! With [`Solver`] implemented, you can now begin solving problems!
//!
//! ## Implementing a Solution
//! For demonstration purposes, we'll assume a simple first problem:
//! - The input is a list of integers, one per line.
//! - Part one wants the sum of all the integers.
//! - Part two wants us to square each integer, *then* sum them.
//!
//! Start by implementing [`Solution<DAY_01>`] for your solutions struct; at minimum, you need to provide
//! type definitions for [`Input`](Solution::Input) and [`Output`](Solution::Output),
//! as well as an implementation of [`parse`](Solution::parse).
//! ``` ignore
//! impl Solution<DAY_01> for Solutions {
//! type Input<'i> = Vec<u64>;
//! type Output = u64;
//!
//! fn parse(puzzle: &str) -> Self::Input<'_> {
//! puzzle
//! .lines()
//! .map(str::parse::<u64>())
//! .map(Result::unwrap)
//! .collect::<Vec<_>>()
//! }
//! }
//! ```
//! At this point, the solution is technically ready to be run. You can use the [`solve_through`] macro to execute
//! all solutions up to a certain day, like so:
//! ``` ignore
//! fn main() {
//! // Notes:
//! // - Due to macro limitations, you must use an integer literal for the day cap.
//! // - Trying to solve through days you haven't implemented yet is a compile error.
//! solve_through!(Solutions, 1);
//! }
//! ```
//! Assuming your [`load`](Solver::load) implementation works, the program should output something like this:
//! ``` shell
//! --- DAY 1 ---
//! Part 1: unimplemented
//! Part 2: unimplemented
//!
//! --- BENCH (RELEASE) ---
//! Parsing: 20 ns
//! Part 1: 20 ns
//! Part 2: 20 ns
//! Total: 60 ns
//! ```
//! It looks like the actual solution logic is unimplemented! Fortunately, that's easy to fix - we just implement
//! the [`part_one`](Solution::part_one) and [`part_two`](Solution::part_two) methods.
//! ``` ignore
//! impl Solution<DAY_01> for Solutions {
//! type Input<'i> = Vec<u64>;
//! type Output = u64;
//!
//! fn parse(puzzle: &str) -> Self::Input<'_> {
//! puzzle
//! .lines()
//! .map(str::parse::<u64>())
//! .map(Result::unwrap)
//! .collect::<Vec<_>>()
//! }
//!
//! fn part_one(input: &Self::Input<'_>) -> Self::Output {
//! input.iter()
//! .sum::<u64>()
//! }
//!
//! fn part_two(input: &Self::Input<'_>) -> Self::Output {
//! input.iter()
//! .map(|x| x.pow(2) )
//! .sum::<u64>()
//! }
//! }
//! ```
//! As you can see, the signatures of the solver methods are identical apart from their names - they take
//! a shared reference to a value of type [`Input`](Solution::Input) and return an [`Output`](Solution::Output).
//!
//! The default implementations of these methods *panic*, which (by using [`std::panic::catch_unwind`]) is how `lib_aoc`
//! knew to display `unimplemented` when the program was run earlier. By overriding them with implementations that
//! *don't* panic and instead return a proper value, the result will be displayed instead:
//! ``` shell
//! --- DAY 1 ---
//! Part 1: 2506
//! Part 2: 95843
//!
//! --- BENCH (RELEASE) ---
//! Parsing: 7.223 µs
//! Part 1: 73.838 µs
//! Part 2: 81.042 µs
//! Total: 162.244 µs
//! ```
//! And that's it - you've implemented a solution!
//!
//! ## Deriving Tests
//! Because Advent of Code provides a test case in the description of every problem, `lib_aoc` also comes with a macro for
//! deriving tests from your [`Solution`] implementations.
//!
//! Assuming your [`load_test`](Solver::load_test) implementation already correctly loads test cases, all you need to do is implement
//! the [`Test`] trait on your solution to provide the expected results:
//! ``` ignore
//! impl Test<DAY_01> for Solutions {
//! fn expected(part: bool) -> Self::Output {
//! // If you don't know the expected result for a part yet, you can just
//! // substitute a panicking macro.
//! match part {
//! // PART_ONE and PART_TWO are constants from the prelude.
//! PART_ONE => 24_000,
//! PART_TWO => 34_000
//! }
//! }
//! }
//! ```
//! Then you can invoke the [`derive_tests`] macro to auto-generate the tests:
//! ``` ignore
//! derive_tests!(Solutions, DAY_01);
//! ```
//! This expands into a new module with a test function for each part of the solution, and can be run normally via `cargo test`.
//!
//! ## Notes on Benchmarking
//! `lib_aoc` provides basic benchmarking of solution implementations via [`std::time::Instant`]. While the
//! measurements it provides are good approximations, a crate like [`criterion`](https://docs.rs/criterion/latest/criterion/)
//! is a better choice if you want a more rigorous solution.
//!
//! Also note that execution clock is started *after* your [`Solver::load`] implementation returns,
//! immediately before [`Solution::parse`] is invoked. This means the time spent loading the puzzle input is not considered
//! by the benchmark.
//!
//! ## Additional Customization Options
//! By overriding the [`Solver::display`] and [`Solver::finalize`] methods, it's possible to define custom behavior
//! that is invoked once a solution finishes executing in a non-test context.
//!
//! [`display`](Solver::display) has a default implementation that pretty-prints the solution outcome,
//! while [`finalize`](Solver::finalize) defaults to a no-op. Both methods take a shared reference to an [`Outcome<impl Display>`].
//!
//! Want to add some awesome extra behavior like submitting your solution to AoC right from the command line? You can do that here!
/// Library prelude; glob-import to bring all important items into scope.
// This re-export is necessary for the solve_through! macro to work.
pub use seq;
use ;
use Outcome;
use Timer;
/// Implements the solution to a single Advent of Code problem.
///
/// Should be implemented on a marker struct (e.g. `struct Solutions {}`);
/// see [the getting started guide](crate) for more information.
/// Marker struct used to indicate panics triggered by unimplemented solutions.
/// Interface for testing Advent of Code puzzle solutions.
///
/// See [the getting started guide](crate) for more information.
/// Interface for running Advent of Code puzzle solutions.
///
/// See [the getting started guide](crate) for more information.
/// Wrapper enum for problems with answer types that differ between parts.
///
/// Example usage:
/// ``` no_run
/// # use lib_aoc::prelude::*;
/// # struct Solutions {}
/// # impl Solver for Solutions {
/// # fn load(day: u8) -> String { panic!() }
/// # fn load_test(day: u8, part: bool) -> String { panic!() }
/// # }
/// use lib_aoc::Split;
///
/// impl Solution<DAY_01> for Solutions {
/// type Input<'i> = usize;
/// type Output = Split<usize, String>;
///
/// fn parse(puzzle: &str) -> Self::Input<'_> {
/// puzzle.parse::<usize>().unwrap()
/// }
///
/// fn part_one(input: &Self::Input<'_>) -> Self::Output {
/// Split::P1(*input)
/// }
///
/// fn part_two(input: &Self::Input<'_>) -> Self::Output {
/// Split::P2(input.to_string())
/// }
/// }