diff --git a/day18-hard/Cargo.toml b/day18-hard/Cargo.toml new file mode 100644 index 0000000..134519b --- /dev/null +++ b/day18-hard/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "day18-hard" +version = "0.1.0" +authors = ["inga-lovinde <52715130+inga-lovinde@users.noreply.github.com>"] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +nom = "^4.2.3" +nom-peg = "0.1.1" diff --git a/day18-hard/src/main.rs b/day18-hard/src/main.rs new file mode 100644 index 0000000..94eeda9 --- /dev/null +++ b/day18-hard/src/main.rs @@ -0,0 +1,31 @@ +use std::io::{self, BufRead}; + +#[macro_use] +extern crate nom; +extern crate nom_peg; +use nom_peg::grammar; + +fn main() { + let arithmetic = grammar! { + parse: i64 = "=" + + expr: i64 = (" ")* "*" (" ")* => { l * r } + | sum + + sum: i64 = (" ")* "+" (" ")* => { l + r } + | value + + value: i64 = ("0"|"1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9")+ => { result.join("").parse::().unwrap() } + | "(" ")" + }; + + let mut result = 0; + let stdin = io::stdin(); + for line in stdin.lock().lines() { + let value = arithmetic.parse(&(line.unwrap() + "=")).unwrap().1; + println!("{}", value); + result += value; + } + + println!("{}", result) +} diff --git a/day18/Cargo.toml b/day18/Cargo.toml new file mode 100644 index 0000000..097bc23 --- /dev/null +++ b/day18/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "day18" +version = "0.1.0" +authors = ["inga-lovinde <52715130+inga-lovinde@users.noreply.github.com>"] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +nom = "^4.2.3" +nom-peg = "0.1.1" diff --git a/day18/src/main.rs b/day18/src/main.rs new file mode 100644 index 0000000..11651b7 --- /dev/null +++ b/day18/src/main.rs @@ -0,0 +1,29 @@ +use std::io::{self, BufRead}; + +#[macro_use] +extern crate nom; +extern crate nom_peg; +use nom_peg::grammar; + +fn main() { + let arithmetic = grammar! { + parse: i64 = "=" + + term: i64 = (" ")* "+" (" ")* => { r + l } + | (" ")* "*" (" ")* => { r * l } + | value + + value: i64 = ("0"|"1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9")+ => { result.join("").chars().rev().collect::().parse::().unwrap() } + | ")" "(" + }; + + let mut result = 0; + let stdin = io::stdin(); + for line in stdin.lock().lines() { + let value = arithmetic.parse(&(line.unwrap().chars().rev().collect::() + "=")).unwrap().1; + println!("{}", value); + result += value; + } + + println!("{}", result) +}