From 8c6e25d0b9c6c85ce0837540700bdd44c012c26b Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Wed, 2 Dec 2020 18:40:32 -0800 Subject: [PATCH] Migrate to using cargo-aoc. --- 2020/Cargo.lock | 106 ++++++++++++++++++ 2020/Cargo.toml | 2 + .../day1-input.txt => input/2020/day1.txt} | 0 2020/src/bin/day1-test.txt | 6 - 2020/src/{bin => }/day1.rs | 97 ++++------------ 2020/src/lib.rs | 5 + 6 files changed, 135 insertions(+), 81 deletions(-) rename 2020/{src/bin/day1-input.txt => input/2020/day1.txt} (100%) delete mode 100644 2020/src/bin/day1-test.txt rename 2020/src/{bin => }/day1.rs (56%) create mode 100644 2020/src/lib.rs diff --git a/2020/Cargo.lock b/2020/Cargo.lock index f2840e8..1357c82 100644 --- a/2020/Cargo.lock +++ b/2020/Cargo.lock @@ -5,6 +5,8 @@ name = "advent2020" version = "0.1.0" dependencies = [ "anyhow", + "aoc-runner", + "aoc-runner-derive", ] [[package]] @@ -12,3 +14,107 @@ name = "anyhow" version = "1.0.34" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bf8dcb5b4bbaa28653b647d8c77bd4ed40183b48882e130c1f1ffb73de069fd7" + +[[package]] +name = "aoc-runner" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d21ef9204ad206a5a3e918e9920da04e1118ad91ce4f23570be964b9d6b9dfcb" + +[[package]] +name = "aoc-runner-derive" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba8b944269d3fee645d281b1335e1797044db497bb02d0098cc3fdb8900069cc" +dependencies = [ + "aoc-runner-internal", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "aoc-runner-internal" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "274b0ba7f3669a45ec0aaacf94eb032a749de880ab776091576cca94037c9982" +dependencies = [ + "serde", + "serde_derive", + "serde_json", +] + +[[package]] +name = "itoa" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6" + +[[package]] +name = "proc-macro2" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "ryu" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" + +[[package]] +name = "serde" +version = "1.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b88fa983de7720629c9387e9f517353ed404164b1e482c970a90c1a4aaf7dc1a" + +[[package]] +name = "serde_derive" +version = "1.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbd1ae72adb44aab48f325a02444a5fc079349a8d804c1fc922aed3f7454c74e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.60" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1500e84d27fe482ed1dc791a56eddc2f230046a040fa908c08bda1d9fb615779" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "syn" +version = "1.0.53" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8833e20724c24de12bbaba5ad230ea61c3eafb05b881c7c9d3cfe8638b187e68" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "unicode-xid" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" diff --git a/2020/Cargo.toml b/2020/Cargo.toml index e948882..11e5ba3 100644 --- a/2020/Cargo.toml +++ b/2020/Cargo.toml @@ -8,3 +8,5 @@ edition = "2018" [dependencies] anyhow = "1.0.34" +aoc-runner = "0.3.0" +aoc-runner-derive = "0.3.0" diff --git a/2020/src/bin/day1-input.txt b/2020/input/2020/day1.txt similarity index 100% rename from 2020/src/bin/day1-input.txt rename to 2020/input/2020/day1.txt diff --git a/2020/src/bin/day1-test.txt b/2020/src/bin/day1-test.txt deleted file mode 100644 index e3fb011..0000000 --- a/2020/src/bin/day1-test.txt +++ /dev/null @@ -1,6 +0,0 @@ -1721 -979 -366 -299 -675 -1456 diff --git a/2020/src/bin/day1.rs b/2020/src/day1.rs similarity index 56% rename from 2020/src/bin/day1.rs rename to 2020/src/day1.rs index b6a2c90..78b1f57 100644 --- a/2020/src/bin/day1.rs +++ b/2020/src/day1.rs @@ -30,98 +30,45 @@ //! //! In your expense report, what is the product of the three entries that sum to 2020? -use std::fs::File; -use std::io::{BufRead, BufReader}; -use std::path::Path; +use aoc_runner_derive::{aoc, aoc_generator}; -use anyhow::{anyhow, Result}; - -fn main() -> Result<()> { - let path = std::env::args() - .nth(1) - .ok_or(anyhow!("Usage: 1 "))?; - let nums = parse(path)?; - let pair = find_pair_2020(&nums).ok_or(anyhow!("Couldn't find pairs summing to 2020"))?; - println!("Product of {} x {} = {}", pair.0, pair.1, pair.0 * pair.1); - let triple = find_triple_2020(&nums).ok_or(anyhow!("Couldn't find triples summing to 2020"))?; - println!( - "Product of {} x {} x {} = {}", - triple.0, - triple.1, - triple.2, - triple.0 * triple.1 * triple.2 - ); - Ok(()) +/// Reads text file containing one integer per line, and parses them into `Vec`. Any +/// non-number will result in a panice. +#[aoc_generator(day1)] +fn parse(input: &str) -> Vec { + input + .split('\n') + .map(|line| line.parse().unwrap()) + .collect() } -/// Finds pairs of numbers in `nums` that sum to 2020. If no pairs are found, `None` is returned. -fn find_pair_2020(nums: &Vec) -> Option<(u32, u32)> { +/// Finds pairs of numbers in `nums` that sum to 2020. If no pairs are found, the function panics. +/// TODO(wathiede): make a version that sorts or uses a hash for finding the match to compare +/// benchmarks. +#[aoc(day1, part1)] +fn find_pair_2020(nums: &[u32]) -> u32 { for (idx, first) in nums.iter().enumerate() { for second in nums.iter().skip(idx + 1) { if first + second == 2020 { - return Some((*first, *second)); + return first * second; } } } - None + panic!("Couldn't find pair"); } -/// Finds triple of numbers in `nums` that sum to 2020. If no triple is found, `None` is returned. -fn find_triple_2020(nums: &Vec) -> Option<(u32, u32, u32)> { +/// Finds triple of numbers in `nums` that sum to 2020. If no triple is found, the function +/// panics. +#[aoc(day1, part2)] +fn find_triple_2020(nums: &[u32]) -> u32 { for (idx1, first) in nums.iter().enumerate() { for (idx2, second) in nums.iter().enumerate().skip(idx1 + 1) { for third in nums.iter().skip(idx2 + 1) { if first + second + third == 2020 { - return Some((*first, *second, *third)); + return first * second * third; } } } } - None -} - -/// Reads text file containing one integer per line, and parses them into `Vec`. Any -/// non-number will result in an error returned. -fn parse>(path: P) -> Result> { - let f = File::open(path)?; - let f = BufReader::new(f); - let mut nums = Vec::new(); - for line in f.lines() { - let num: u32 = line?.parse()?; - nums.push(num) - } - Ok(nums) -} - -#[cfg(test)] -mod tests { - use anyhow::Context; - - use super::*; - - fn get_nums() -> Vec { - let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")); - let path = root.join("src/bin/day1-test.txt"); - parse(&path) - .with_context(|| format!("Input {}", path.display())) - .expect("failed to parse") - } - - #[test] - fn test_parse() { - let nums = get_nums(); - assert_eq!(nums, vec![1721, 979, 366, 299, 675, 1456]); - } - - #[test] - fn test_find_pair_2020() { - let nums = get_nums(); - assert_eq!(find_pair_2020(&nums), Some((1721, 299))); - } - - #[test] - fn test_find_triple_2020() { - let nums = get_nums(); - assert_eq!(find_triple_2020(&nums), Some((979, 366, 675))); - } + panic!("Couldn't find triple"); } diff --git a/2020/src/lib.rs b/2020/src/lib.rs new file mode 100644 index 0000000..ebded38 --- /dev/null +++ b/2020/src/lib.rs @@ -0,0 +1,5 @@ +mod day1; + +use aoc_runner_derive::aoc_lib; + +aoc_lib! { year = 2020 }