base on null # Cargo Advent of Code Helper `cargo-aoc` is a simple CLI tool that aims to be a helper for the [Advent of Code](https://adventofcode.com). Implement your solution. Let us handle the rest. # Features * Input downloading * Running your solution * Automatic benchmarking of your solution using [Criterion](https://github.com/japaric/criterion.rs) # Getting started ## Install `cargo aoc` `cargo-aoc` is hosted as a binary on crates.io. Boot a terminal and install the program using `cargo install cargo-aoc` ## Setting up the CLI You will need to find your session token for the AoC in order for cargo-aoc to work. Thankfully, finding your token is easy since it is stored in your Browser's cookies. Open up the devtools of your browser, and then : * Firefox: "Storage" tab, Cookies, and copy the "Value" field of the `session` cookie. * Google Chrome / Chromium: "Application" tab, Cookies, and copy the "Value" field of the `session` cookie. Once you have it, simply run : `cargo aoc credentials {token}` You're now ready to start coding ! NOTE: If for some reason your token has changed, dont forget to change it back. `cargo aoc credentials` will show the currently stored user token ## Setting up the project In order for `cargo-aoc` to work properly, you have to set the project up correctly. If you get lost during the process, you can take [this example repository of AoC 2015](https://github.com/gobanos/advent-of-code-2015) as a template. First, you must add a dependency on `aoc-runner` and `aoc-runner-derive` in your `Cargo.toml`. At the end of the `src/lib.rs`, you will have to use the macro `aoc_lib!{ year = XXXX }`, where XXXX is the year of the AoC puzzles being solved. When implementing a solution for a day, you have to provide functions and tag them accordingly. A function is either a **solver** or a **generator**. Those two types of functions are being executed and benchmarked seperately. Lets have a closer look : ### Generator functions Generators allows you to provide a custom type to the solver functions. Sometimes in AoC, you have to parse an input and extract a logical structure out of it, before you can actually solve the problem. Generator functions are tagged `#[aoc_generator(dayX)]`. Because examples are worth a thousand words, lets take a look at [Year 2015, Day 2](https://adventofcode.com/2015/day/2) : From the puzzle's description, we know that `[we] have a list of the dimensions (length l, width w, and height h) of each present`, each present on one line, represented like so: `{L}x{W}x{H}`. We might want to first parse the input and extract logical `Gift` structs out of it, like: ``` pub struct Gift { l: u32, w: u32, h: u32 } ``` In @Gobanos' reference implementation, we can see that he instead chose to settle for a custom type : `type Gift = (u32, u32, u32);`. Thus, writing a generator for `Gift`s is fairly simple: ``` #[aoc_generator(day2)] pub fn input_generator(input: &str) -> Vec<Gift> { input .lines() .map(|l| { let mut gift = l.trim().split('x').map(|d| d.parse().unwrap()); ( gift.next().unwrap(), gift.next().unwrap(), gift.next().unwrap(), ) }).collect() } ``` As you can see, generators take a `&str` (or a `&[u8]`) type as an input, and outputs any type that you want, so you can then use it in `solver` functions. [link to doc](https://docs.rs/aoc-runner-derive/latest/aoc_runner_derive/attr.aoc_generator.html) ### Solver functions Solver functions are typically your algorithms, they take any input type provided by a generator, and return any type that you want to use, provided that it implements the `Display` trait. Solver functions are tagged `#[aoc(day2, part1)]`. Optionally, you can have multiple implementation for the same part of a day. You must then use a name to tag them correctly, for example : `#[aoc(day2, part1, for_loop)]`. Following with the previous example, implementing a solver for the part one could be done like this : ``` #[aoc(day2, part1)] pub fn solve_part1(input: &[Gift]) -> u32 { input .iter() .map(|&(l, w, h)| { let (s1, s2) = smallest_side((l, w, h)); 2 * l * w + 2 * w * h + 2 * h * l + s1 * s2 }) .sum() } ``` Notice how we're taking the `Gift`s generated previously, and using Rust's iterators to solve the problem efficiently, all the while keeping the code maintainable. The output of this particular solver is an `u32`, which of course implements `Display`. When running your solution using `cargo aoc`, said result will then get printed in the console, along with other informations about execution time. [link to doc](https://docs.rs/aoc-runner-derive/latest/aoc_runner_derive/attr.aoc.html) # Downloading your input manually `cargo aoc input` will download an input and store it in `input/{year}/day{day}.txt`. Please note that by default, we're taking today's date as the argument. Of course, you can change this using : `cargo aoc input -d {day} -y {year}` # Running your solution `cargo aoc` will run the latest implemented day, downloading your input beforehand. It will show you the result, and a short summary of how well it did perform. Example output on my Chromebook, running [@Gobanos' AOC2015](https://github.com/gobanos/advent-of-code-2015) : ``` [olivier@olivier-pc advent-of-code-2015]$ cargo aoc Finished dev [unoptimized + debuginfo] target(s) in 0.12s Compiling aoc-autobuild v0.1.0 (/home/olivier/Workspace/Rust/advent-of-code-2015/target/aoc/aoc-autobuild) Finished release [optimized] target(s) in 0.87s Running `target/release/aoc-autobuild` AOC 2015 Day 5 - Part 1 : 238 generator: 18.122µs, runner: 420.958µs Day 5 - Part 2 : 69 generator: 5.499µs, runner: 1.142373ms ``` If you want to run an older puzzle, or only a specific part, specify those using `cargo aoc -d {day} -p {part}`. # Benchmarking your solution Benchmarking is powered by [Criterion](https://github.com/japaric/criterion.rs). Use `cargo aoc bench` to launch the benchmarks, just like you would use `cargo aoc`. Benchmarks for each days are then generated in `target/aoc/aoc-autobench/target/criterion`. You can open the benchmark automatically in your Browser afterwards, using `cargo aoc bench -o` Soon(tm), you will also be able to use our (free) online platform, to compare your results with those of the community. ------ Happy Advent of Code ! ", Assign "at most 3 tags" to the expected json: {"id":"5533","tags":[]} "only from the tags list I provide: [{"id":77,"name":"3d"},{"id":89,"name":"agent"},{"id":17,"name":"ai"},{"id":54,"name":"algorithm"},{"id":24,"name":"api"},{"id":44,"name":"authentication"},{"id":3,"name":"aws"},{"id":27,"name":"backend"},{"id":60,"name":"benchmark"},{"id":72,"name":"best-practices"},{"id":39,"name":"bitcoin"},{"id":37,"name":"blockchain"},{"id":1,"name":"blog"},{"id":45,"name":"bundler"},{"id":58,"name":"cache"},{"id":21,"name":"chat"},{"id":49,"name":"cicd"},{"id":4,"name":"cli"},{"id":64,"name":"cloud-native"},{"id":48,"name":"cms"},{"id":61,"name":"compiler"},{"id":68,"name":"containerization"},{"id":92,"name":"crm"},{"id":34,"name":"data"},{"id":47,"name":"database"},{"id":8,"name":"declarative-gui "},{"id":9,"name":"deploy-tool"},{"id":53,"name":"desktop-app"},{"id":6,"name":"dev-exp-lib"},{"id":59,"name":"dev-tool"},{"id":13,"name":"ecommerce"},{"id":26,"name":"editor"},{"id":66,"name":"emulator"},{"id":62,"name":"filesystem"},{"id":80,"name":"finance"},{"id":15,"name":"firmware"},{"id":73,"name":"for-fun"},{"id":2,"name":"framework"},{"id":11,"name":"frontend"},{"id":22,"name":"game"},{"id":81,"name":"game-engine "},{"id":23,"name":"graphql"},{"id":84,"name":"gui"},{"id":91,"name":"http"},{"id":5,"name":"http-client"},{"id":51,"name":"iac"},{"id":30,"name":"ide"},{"id":78,"name":"iot"},{"id":40,"name":"json"},{"id":83,"name":"julian"},{"id":38,"name":"k8s"},{"id":31,"name":"language"},{"id":10,"name":"learning-resource"},{"id":33,"name":"lib"},{"id":41,"name":"linter"},{"id":28,"name":"lms"},{"id":16,"name":"logging"},{"id":76,"name":"low-code"},{"id":90,"name":"message-queue"},{"id":42,"name":"mobile-app"},{"id":18,"name":"monitoring"},{"id":36,"name":"networking"},{"id":7,"name":"node-version"},{"id":55,"name":"nosql"},{"id":57,"name":"observability"},{"id":46,"name":"orm"},{"id":52,"name":"os"},{"id":14,"name":"parser"},{"id":74,"name":"react"},{"id":82,"name":"real-time"},{"id":56,"name":"robot"},{"id":65,"name":"runtime"},{"id":32,"name":"sdk"},{"id":71,"name":"search"},{"id":63,"name":"secrets"},{"id":25,"name":"security"},{"id":85,"name":"server"},{"id":86,"name":"serverless"},{"id":70,"name":"storage"},{"id":75,"name":"system-design"},{"id":79,"name":"terminal"},{"id":29,"name":"testing"},{"id":12,"name":"ui"},{"id":50,"name":"ux"},{"id":88,"name":"video"},{"id":20,"name":"web-app"},{"id":35,"name":"web-server"},{"id":43,"name":"webassembly"},{"id":69,"name":"workflow"},{"id":87,"name":"yaml"}]" returns me the "expected json"