|
| 1 | +Advent of Code helper |
| 2 | +===================== |
| 3 | + |
| 4 | +This crate provides a macro to generate a handful main for your [advent of |
| 5 | +code][1] participation. The intention is to provide something similar to |
| 6 | +[cargo-aoc][2] through a much simple code base. |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +Currently this will only provide a few benefits: |
| 11 | + |
| 12 | + - a somewhat pretty orchestration of your solutions |
| 13 | + - automatic download and loading of your input files |
| 14 | + |
| 15 | + |
| 16 | +Usage |
| 17 | +----- |
| 18 | + |
| 19 | +The generator and solutions for a given day must be implemented in a module |
| 20 | +called `dayXX`. Then you can simply invoke the `aoc::main!` macro in your |
| 21 | +`main.rs`: |
| 22 | + |
| 23 | +```rust |
| 24 | +mod day1 { |
| 25 | + pub fn generator(input: &str) -> Vec<u64> { |
| 26 | + input |
| 27 | + .lines() |
| 28 | + .map(|line| line.parse().unwrap()) |
| 29 | + .collect() |
| 30 | + } |
| 31 | + |
| 32 | + pub fn part_1(input: &[u64]) -> u64 { |
| 33 | + input.iter().map(|&mass| mass / 3 - 2).sum() |
| 34 | + } |
| 35 | + |
| 36 | + pub fn part_2(input: &[u64]) -> u64 { |
| 37 | + fn total_needed_mass(obj: u64) -> u64 { |
| 38 | + if obj < 9 { |
| 39 | + 0 |
| 40 | + } else { |
| 41 | + let obj_mass = obj / 3 - 2; |
| 42 | + obj_mass + total_needed_mass(obj_mass) |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + input.iter().copied().map(total_needed_mass).sum() |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +mod day2; |
| 51 | + |
| 52 | +aoc::main! { |
| 53 | + year 2019; |
| 54 | + day1 : generator => part_1, part_2; |
| 55 | + day2 : generator => part_1, part_2, part_2_optimized; |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +Then you can simply run `cargo run`! |
| 60 | + |
| 61 | +Note that your solutions must take a borrowed version of the generator's |
| 62 | +output. Also, the generator can output a structure that contains borrowed data |
| 63 | +from the original input file. |
| 64 | + |
| 65 | + |
| 66 | +Fetching your AOC token |
| 67 | +----------------------- |
| 68 | + |
| 69 | +When the command line asks you for your session token, you can apply the |
| 70 | +following procedure (quoted from [cargo-aoc][2]): |
| 71 | + |
| 72 | + - Firefox: "Storage" tab, Cookies, and copy the "Value" field of the session |
| 73 | + cookie. |
| 74 | + - Google Chrome / Chromium: "Application" tab, Cookies, and copy the "Value" |
| 75 | + field of the session cookie. |
| 76 | + |
| 77 | + |
| 78 | +[1]: https://adventofcode.com |
| 79 | +[2]: https://github.com/gobanos/cargo-aoc |
0 commit comments