diff options
author | cos <cos> | 2020-12-01 08:01:25 +0100 |
---|---|---|
committer | cos <cos> | 2020-12-01 11:41:34 +0100 |
commit | f44158d941198d175c26ef8f74e80e4d4d0fa7a2 (patch) | |
tree | 199a394c07fb5ceba511a80432bc1ef37a1f7d23 | |
parent | 7b0a1642a13077d5931469d6281ea20a98ec0ace (diff) | |
download | adventofcode-f44158d941198d175c26ef8f74e80e4d4d0fa7a2.zip |
Add day01, 2020
-rw-r--r-- | 2020/rust/Cargo.toml | 28 | ||||
-rw-r--r-- | 2020/rust/day01/Cargo.toml | 8 | ||||
-rw-r--r-- | 2020/rust/day01/src/main.rs | 72 |
3 files changed, 108 insertions, 0 deletions
diff --git a/2020/rust/Cargo.toml b/2020/rust/Cargo.toml new file mode 100644 index 0000000..9674b94 --- /dev/null +++ b/2020/rust/Cargo.toml @@ -0,0 +1,28 @@ +[workspace] +members = [ + "day01", +# "day02", +# "day03", +# "day04", +# "day05", +# "day06", +# "day07", +# "day08", +# "day09", +# "day10", +# "day11", +# "day12", +# "day13", +# "day14", +# "day15", +# "day16", +# "day17", +# "day18", +# "day19", +# "day20", +# "day21", +# "day22", +# "day23", +# "day24", +# "day25", +] diff --git a/2020/rust/day01/Cargo.toml b/2020/rust/day01/Cargo.toml new file mode 100644 index 0000000..674217d --- /dev/null +++ b/2020/rust/day01/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day01" +version = "0.1.0" +authors = ["cos <cos>"] +edition = "2018" + +[dependencies] +anyhow = "1.0" diff --git a/2020/rust/day01/src/main.rs b/2020/rust/day01/src/main.rs new file mode 100644 index 0000000..38f25d2 --- /dev/null +++ b/2020/rust/day01/src/main.rs @@ -0,0 +1,72 @@ +use anyhow::Result; +use std::env::args; +use std::fs::File; +use std::io::{BufRead, BufReader}; +use std::path::Path; + +const SUM: usize = 2020; + +fn read_input<T: AsRef<Path>>(filename: T) -> Result<Vec<usize>> { + let f = File::open(filename)?; + let reader = BufReader::new(f); + + let values = reader.lines() + .map(|v| v.unwrap().parse::<usize>().unwrap()).collect(); + + Ok(values) +} + +fn part1(input: &[usize]) -> Option<usize> { + for first in input { + for second in input { + if first + second == SUM { + return Some(first * second); + } + } + } + + None +} + +fn part2(input: &[usize]) -> Option<usize> { + for first in input { + for second in input { + for third in input { + if first + second + third == SUM { + return Some(first * second * third); + } + } + } + } + + None +} + +fn main() { + let filename = match args().nth(1) { + Some(f) => f, + None => { + eprintln!("Missing input filename"); + std::process::exit(1); + }, + }; + match read_input(filename) { + Ok(input) => { + match part1(&input) { + Some(solution) => println!("Part1, product found to be: {}", solution), + None => { + eprintln!("Part1, no solution found"); + std::process::exit(1); + } + }; + match part2(&input) { + Some(solution) => println!("Part2, product found to be: {}", solution), + None => { + eprintln!("Part2, no solution found"); + std::process::exit(1); + } + }; + }, + Err(err) => eprintln!("Could not read input: {}", err), + } +} |