Day 3
This commit is contained in:
parent
d42778af2a
commit
7a38d30449
45
Cargo.lock
generated
45
Cargo.lock
generated
@ -2,6 +2,15 @@
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "1.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.13.0"
|
||||
@ -17,9 +26,45 @@ dependencies = [
|
||||
"either",
|
||||
]
|
||||
|
||||
[[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 = "testing"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"itertools",
|
||||
"regex",
|
||||
]
|
||||
|
@ -12,3 +12,4 @@ all = "warn"
|
||||
|
||||
[dependencies]
|
||||
itertools = "0.13.0"
|
||||
regex = "1.11.1"
|
||||
|
41
src/main.rs
41
src/main.rs
@ -101,9 +101,46 @@ mod day2 {
|
||||
}
|
||||
}
|
||||
|
||||
mod day3 {
|
||||
use std::fs::read_to_string;
|
||||
|
||||
use regex::Regex;
|
||||
|
||||
pub fn task1() {
|
||||
let rx = Regex::new(r"mul\((\d{1,3}),(\d{1,3})\)").unwrap();
|
||||
let mut result: i64 = 0;
|
||||
for c in rx.captures_iter(&read_to_string("input.txt").unwrap()) {
|
||||
let (_, [a, b]) = c.extract();
|
||||
result += a.parse::<i64>().unwrap() * b.parse::<i64>().unwrap();
|
||||
}
|
||||
println!("{result}");
|
||||
}
|
||||
|
||||
pub fn task2() {
|
||||
let rx = Regex::new(r"(mul)\((\d{1,3}),(\d{1,3})\)|do()()()\(\)|do(n't)()()\(\)").unwrap();
|
||||
let mut enabled = true;
|
||||
let mut result: i64 = 0;
|
||||
for c in rx.captures_iter(&read_to_string("input.txt").unwrap()) {
|
||||
let (_, [op, a, b]) = c.extract();
|
||||
match op {
|
||||
"" => enabled = true,
|
||||
"n't" => enabled = false,
|
||||
"mul" if enabled => {
|
||||
result += a.parse::<i64>().unwrap() * b.parse::<i64>().unwrap();
|
||||
}
|
||||
"mul" => {}
|
||||
_ => unreachable!("{op}"),
|
||||
}
|
||||
}
|
||||
println!("{result}");
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// day1::task1();
|
||||
// day1::task1();
|
||||
day2::task1();
|
||||
day2::task2();
|
||||
// day2::task1();
|
||||
// day2::task2();
|
||||
day3::task1();
|
||||
day3::task2();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user