From 50b32531eee1b1c4bca427af453fbdf474746c3e Mon Sep 17 00:00:00 2001 From: Kane York Date: Tue, 1 Dec 2020 23:10:00 -0800 Subject: feat(u/riking/aoc/day01): add day01 solution Tests with the puzzle's sample inputs are included. Change-Id: I32cd59b9e3894bde3d67ebdc3a977961b17bdb49 Reviewed-on: https://cl.tvl.fyi/c/depot/+/2223 Reviewed-by: kanepyork Tested-by: BuildkiteCI --- users/riking/adventofcode-2020/.gitignore | 2 + users/riking/adventofcode-2020/day01/Cargo.lock | 14 ++++ users/riking/adventofcode-2020/day01/Cargo.toml | 10 +++ users/riking/adventofcode-2020/day01/default.nix | 10 +++ users/riking/adventofcode-2020/day01/src/main.rs | 85 ++++++++++++++++++++++++ 5 files changed, 121 insertions(+) create mode 100644 users/riking/adventofcode-2020/.gitignore create mode 100644 users/riking/adventofcode-2020/day01/Cargo.lock create mode 100644 users/riking/adventofcode-2020/day01/Cargo.toml create mode 100644 users/riking/adventofcode-2020/day01/default.nix create mode 100644 users/riking/adventofcode-2020/day01/src/main.rs (limited to 'users/riking') diff --git a/users/riking/adventofcode-2020/.gitignore b/users/riking/adventofcode-2020/.gitignore new file mode 100644 index 000000000000..076ff412156a --- /dev/null +++ b/users/riking/adventofcode-2020/.gitignore @@ -0,0 +1,2 @@ +*/target +*/input.txt diff --git a/users/riking/adventofcode-2020/day01/Cargo.lock b/users/riking/adventofcode-2020/day01/Cargo.lock new file mode 100644 index 000000000000..a1a18948a7ea --- /dev/null +++ b/users/riking/adventofcode-2020/day01/Cargo.lock @@ -0,0 +1,14 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "anyhow" +version = "1.0.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf8dcb5b4bbaa28653b647d8c77bd4ed40183b48882e130c1f1ffb73de069fd7" + +[[package]] +name = "day01" +version = "0.1.0" +dependencies = [ + "anyhow", +] diff --git a/users/riking/adventofcode-2020/day01/Cargo.toml b/users/riking/adventofcode-2020/day01/Cargo.toml new file mode 100644 index 000000000000..d90ab548bb7b --- /dev/null +++ b/users/riking/adventofcode-2020/day01/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "day01" +version = "0.1.0" +authors = ["Kane York "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +anyhow = "1.0.34" diff --git a/users/riking/adventofcode-2020/day01/default.nix b/users/riking/adventofcode-2020/day01/default.nix new file mode 100644 index 000000000000..0648a05af683 --- /dev/null +++ b/users/riking/adventofcode-2020/day01/default.nix @@ -0,0 +1,10 @@ +{ depot, ... }: + +with depot.third_party; + +naersk.buildPackage { + src = ./.; + + buildInputs = []; + doCheck = true; +} diff --git a/users/riking/adventofcode-2020/day01/src/main.rs b/users/riking/adventofcode-2020/day01/src/main.rs new file mode 100644 index 000000000000..3e6b339d7c2b --- /dev/null +++ b/users/riking/adventofcode-2020/day01/src/main.rs @@ -0,0 +1,85 @@ +use anyhow::anyhow; +use std::fs::File; +use std::io::prelude::*; +use std::io::BufReader; + +const PART_2: bool = true; + +fn day01(is_part2: bool, numbers: &Vec) -> Result { + //println!("{:?}", numbers); + + for n1 in numbers.iter() { + for n2 in numbers.iter() { + if is_part2 { + for n3 in numbers.iter() { + if n1 + n2 + n3 == 2020 { + return Ok((n1 * n2 * n3).to_string()); + } + } + } else { + if n1 + n2 == 2020 { + return Ok((n1 * n2).to_string()); + } + } + } + } + + Err(anyhow!("no solution found")) +} + +fn parse(filename: &str) -> Result, anyhow::Error> { + let f = File::open(filename)?; + let mut reader = BufReader::new(f); + + let mut values = Vec::::new(); + + let mut line = String::new(); + loop { + line.clear(); + reader.read_line(&mut line)?; + let trimmed_line = line.trim(); + if trimmed_line.is_empty() { + break; + } + + values.push(trimmed_line.parse()?); + } + Ok(values) +} + +fn main() -> anyhow::Result<()> { + let args: Vec = std::env::args().collect(); + + //println!("{:?}", args); + if args.len() != 2 { + return Err(anyhow!("usage: day01 input_file")); + } + let filename = args.into_iter().skip(1).next().expect("args len == 1"); + + let numbers = parse(&filename)?; + + println!("{}", day01(PART_2, &numbers)?); + + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::day01; + + #[test] + fn test_part1() { + let vec = vec![1721, 979, 366, 299, 675, 1456]; + let result = day01(false, &vec).unwrap(); + + assert_eq!(result, 514579.to_string()); + } + + #[test] + fn test_part2() { + let vec = vec![1721, 979, 366, 299, 675, 1456]; + let result = day01(true, &vec).unwrap(); + + assert_eq!(result, 241861950.to_string()); + } +} -- cgit 1.4.1