From bf79c7262e3c21ce6d895aa6ae22a8c256156694 Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Sun, 2 Dec 2018 09:13:28 -0800 Subject: [PATCH] [2018][rust][1.0] --- .gitignore | 1 + 2018/rust/Cargo.lock | 4 ++++ 2018/rust/Cargo.toml | 7 +++++++ 2018/rust/src/main.rs | 22 ++++++++++++++++++++++ 4 files changed, 34 insertions(+) create mode 100644 .gitignore create mode 100644 2018/rust/Cargo.lock create mode 100644 2018/rust/Cargo.toml create mode 100644 2018/rust/src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..680a797 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +**/rust/target diff --git a/2018/rust/Cargo.lock b/2018/rust/Cargo.lock new file mode 100644 index 0000000..ec1441e --- /dev/null +++ b/2018/rust/Cargo.lock @@ -0,0 +1,4 @@ +[[package]] +name = "advent_of_code_2018" +version = "0.1.0" + diff --git a/2018/rust/Cargo.toml b/2018/rust/Cargo.toml new file mode 100644 index 0000000..0d393ec --- /dev/null +++ b/2018/rust/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "advent_of_code_2018" +version = "0.1.0" +authors = ["Alpha Chen "] +edition = "2018" + +[dependencies] diff --git a/2018/rust/src/main.rs b/2018/rust/src/main.rs new file mode 100644 index 0000000..3acbb2e --- /dev/null +++ b/2018/rust/src/main.rs @@ -0,0 +1,22 @@ +use std::io::{self, Read}; + +fn day_1(input: &str, delimiter: &str) -> String { + let sum: i32 = input + .split(delimiter) + .flat_map(|change| change.parse::().ok()) + .sum(); + sum.to_string() +} + +#[test] +fn test_day_1() { + assert_eq!(day_1("+1, -2, +3, +1", ", "), "3"); +} + +fn main() { + let mut input = String::new(); + io::stdin().read_to_string(&mut input).unwrap(); + + let output = day_1(&input, "\n"); + println!("{}", output); +}