Day3
This commit is contained in:
54
day3/rust/Cargo.lock
generated
Normal file
54
day3/rust/Cargo.lock
generated
Normal file
@@ -0,0 +1,54 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "1.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.7.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.11.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-automata",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-automata"
|
||||
version = "0.4.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.8.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
|
||||
|
||||
[[package]]
|
||||
name = "rust"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"regex",
|
||||
]
|
||||
7
day3/rust/Cargo.toml
Normal file
7
day3/rust/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "rust"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
regex = "1.11.1"
|
||||
45
day3/rust/src/main.rs
Normal file
45
day3/rust/src/main.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
use std::io::Read;
|
||||
|
||||
use regex::Regex;
|
||||
|
||||
fn main() {
|
||||
let mut stdin = std::io::stdin();
|
||||
|
||||
let mut input = String::new();
|
||||
stdin.read_to_string(&mut input).unwrap();
|
||||
|
||||
let regex = Regex::new(r"mul\((\d{1,3}),(\d{1,3})\)|do\(\)|don't\(\)").unwrap();
|
||||
|
||||
let mut enabled = true;
|
||||
|
||||
let mut sum = 0;
|
||||
let mut sum2 = 0;
|
||||
for capture in regex.captures_iter(&input) {
|
||||
let matched = capture.get(0).unwrap().as_str();
|
||||
match matched {
|
||||
"do()" => {
|
||||
enabled = true;
|
||||
continue;
|
||||
}
|
||||
"don't()" => {
|
||||
enabled = false;
|
||||
continue;
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
|
||||
let (a, b): (isize, isize) = (
|
||||
capture.get(1).unwrap().as_str().parse().unwrap(),
|
||||
capture.get(2).unwrap().as_str().parse().unwrap(),
|
||||
);
|
||||
|
||||
sum += a * b;
|
||||
|
||||
if enabled {
|
||||
sum2 += a * b;
|
||||
}
|
||||
}
|
||||
|
||||
println!("{}", sum);
|
||||
println!("{}", sum2);
|
||||
}
|
||||
Reference in New Issue
Block a user