From d71c33cecb11d2667f8f6bdd31528cf93160ca80 Mon Sep 17 00:00:00 2001 From: Codinget Date: Tue, 3 Dec 2024 20:20:15 +0000 Subject: [PATCH] day 3, rust --- 03/Cargo.lock | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ 03/Cargo.toml | 7 ++++++ 03/src/first.rs | 20 ++++++++++++++++ 03/src/main.rs | 30 ++++++++++++++++++++++++ 03/src/second.rs | 34 +++++++++++++++++++++++++++ 5 files changed, 152 insertions(+) create mode 100644 03/Cargo.lock create mode 100644 03/Cargo.toml create mode 100644 03/src/first.rs create mode 100644 03/src/main.rs create mode 100644 03/src/second.rs diff --git a/03/Cargo.lock b/03/Cargo.lock new file mode 100644 index 0000000..7311acb --- /dev/null +++ b/03/Cargo.lock @@ -0,0 +1,61 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aoc2024-03" +version = "0.1.0" +dependencies = [ + "peg", +] + +[[package]] +name = "peg" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "295283b02df346d1ef66052a757869b2876ac29a6bb0ac3f5f7cd44aebe40e8f" +dependencies = [ + "peg-macros", + "peg-runtime", +] + +[[package]] +name = "peg-macros" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bdad6a1d9cf116a059582ce415d5f5566aabcd4008646779dab7fdc2a9a9d426" +dependencies = [ + "peg-runtime", + "proc-macro2", + "quote", +] + +[[package]] +name = "peg-runtime" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3aeb8f54c078314c2065ee649a7241f46b9d8e418e1a9581ba0546657d7aa3a" + +[[package]] +name = "proc-macro2" +version = "1.0.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "unicode-ident" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" diff --git a/03/Cargo.toml b/03/Cargo.toml new file mode 100644 index 0000000..3dcf56a --- /dev/null +++ b/03/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "aoc2024-03" +version = "0.1.0" +edition = "2021" + +[dependencies] +peg="0.8.4" \ No newline at end of file diff --git a/03/src/first.rs b/03/src/first.rs new file mode 100644 index 0000000..2090996 --- /dev/null +++ b/03/src/first.rs @@ -0,0 +1,20 @@ +peg::parser! { + grammar mul() for str { + rule number() -> u32 + = n:$(['0'..='9']+) {? n.parse().or(Err("u32")) } + + rule mul_fn() -> u32 + = "mul(" a:number() "," b:number() ")" { a * b } + + rule step() -> u32 + = n:mul_fn() { n } + / [_] { 0 } + + pub rule program() -> u32 + = x:step()+ { x.iter().sum() } + } +} + +pub fn parse(a: &str) -> u32 { + mul::program(a).unwrap() +} diff --git a/03/src/main.rs b/03/src/main.rs new file mode 100644 index 0000000..ef6a571 --- /dev/null +++ b/03/src/main.rs @@ -0,0 +1,30 @@ +mod first; +mod second; + +fn main() { + let raw = std::fs::read_to_string("input.txt").expect("Couldn't read input"); + let first = first::parse(&raw); + println!("{}", first); + let second: u32 = second::parse(&raw) + .iter() + .scan(true, |p, i| match i { + second::Item::Do => { + *p = true; + Some(None) + } + second::Item::Dont => { + *p = false; + Some(None) + } + second::Item::Mul(n) => { + if *p { + Some(Some(n)) + } else { + Some(None) + } + } + }) + .flatten() + .sum(); + println!("{}", second); +} diff --git a/03/src/second.rs b/03/src/second.rs new file mode 100644 index 0000000..f386ede --- /dev/null +++ b/03/src/second.rs @@ -0,0 +1,34 @@ +pub enum Item { + Do, + Dont, + Mul(u32), +} + +peg::parser! { + grammar mul() for str { + rule number() -> u32 + = n:$(['0'..='9']+) {? n.parse().or(Err("u32")) } + + rule mul_fn() -> Item + = "mul(" a:number() "," b:number() ")" { Item::Mul(a * b) } + + rule do_fn() -> Item + = "do()" { Item::Do } + + rule dont_fn() -> Item + = "don't()" { Item::Dont } + + rule step() -> Option + = n:mul_fn() { Some(n) } + / n:do_fn() { Some(n) } + / n:dont_fn() { Some(n) } + / [_] { None } + + pub rule program() -> Vec + = x:step()+ { x.into_iter().flatten().collect() } + } +} + +pub fn parse(a: &str) -> Vec { + mul::program(a).unwrap() +}