parent
a160390483
commit
d71c33cecb
@ -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" |
@ -0,0 +1,7 @@ |
||||
[package] |
||||
name = "aoc2024-03" |
||||
version = "0.1.0" |
||||
edition = "2021" |
||||
|
||||
[dependencies] |
||||
peg="0.8.4" |
@ -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() |
||||
} |
@ -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); |
||||
} |
@ -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<Item> |
||||
= n:mul_fn() { Some(n) } |
||||
/ n:do_fn() { Some(n) } |
||||
/ n:dont_fn() { Some(n) } |
||||
/ [_] { None } |
||||
|
||||
pub rule program() -> Vec<Item> |
||||
= x:step()+ { x.into_iter().flatten().collect() } |
||||
} |
||||
} |
||||
|
||||
pub fn parse(a: &str) -> Vec<Item> { |
||||
mul::program(a).unwrap() |
||||
} |
Loading…
Reference in new issue