Day 3
This commit is contained in:
parent
8360196d0f
commit
8f6f15c473
3 changed files with 108 additions and 0 deletions
33
Cargo.lock
generated
33
Cargo.lock
generated
|
|
@ -17,6 +17,15 @@ version = "1.0.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
|
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "aho-corasick"
|
||||||
|
version = "1.1.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
|
||||||
|
dependencies = [
|
||||||
|
"memchr",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "anstream"
|
name = "anstream"
|
||||||
version = "0.6.18"
|
version = "0.6.18"
|
||||||
|
|
@ -75,6 +84,7 @@ dependencies = [
|
||||||
"color-eyre",
|
"color-eyre",
|
||||||
"humantime",
|
"humantime",
|
||||||
"itertools",
|
"itertools",
|
||||||
|
"regex",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
@ -318,11 +328,34 @@ dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[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]]
|
[[package]]
|
||||||
name = "regex-automata"
|
name = "regex-automata"
|
||||||
version = "0.4.9"
|
version = "0.4.9"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908"
|
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]]
|
[[package]]
|
||||||
name = "rustc-demangle"
|
name = "rustc-demangle"
|
||||||
|
|
|
||||||
|
|
@ -10,3 +10,4 @@ clap = { version = "4.5.21", features = ["derive"] }
|
||||||
color-eyre = "0.6.3"
|
color-eyre = "0.6.3"
|
||||||
humantime = "2.1.0"
|
humantime = "2.1.0"
|
||||||
itertools = "0.13.0"
|
itertools = "0.13.0"
|
||||||
|
regex = "1.11.1"
|
||||||
|
|
|
||||||
74
src/bin/day3.rs
Normal file
74
src/bin/day3.rs
Normal file
|
|
@ -0,0 +1,74 @@
|
||||||
|
use std::time::Instant;
|
||||||
|
|
||||||
|
use aoc_2024::{load, print_res};
|
||||||
|
use bstr::BString;
|
||||||
|
use regex::Regex;
|
||||||
|
|
||||||
|
type Parsed<'a> = &'a str;
|
||||||
|
|
||||||
|
pub fn parsing(input: &BString) -> color_eyre::Result<Parsed> {
|
||||||
|
core::str::from_utf8(input).map_err(Into::into)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn part1(input: Parsed) {
|
||||||
|
let regex = Regex::new(r#"mul\((\d+),(\d+)\)"#).unwrap();
|
||||||
|
let mut total = 0;
|
||||||
|
|
||||||
|
for capture in regex.captures_iter(input) {
|
||||||
|
let first: u64 = capture.get(1).unwrap().as_str().parse().unwrap();
|
||||||
|
let second: u64 = capture.get(2).unwrap().as_str().parse().unwrap();
|
||||||
|
|
||||||
|
total += first * second;
|
||||||
|
}
|
||||||
|
|
||||||
|
print_res!("Sum of multiplications: {total}");
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn part2(input: Parsed) {
|
||||||
|
let regex = Regex::new(r#"don't\(\)|do\(\)|mul\((\d+),(\d+)\)"#).unwrap();
|
||||||
|
let mut total = 0;
|
||||||
|
let mut enabled = true;
|
||||||
|
|
||||||
|
for capture in regex.captures_iter(input) {
|
||||||
|
let instr = capture.get(0).unwrap().as_str();
|
||||||
|
|
||||||
|
match instr {
|
||||||
|
"do()" => enabled = true,
|
||||||
|
"don't()" => enabled = false,
|
||||||
|
// `mul(a,b)`
|
||||||
|
_ => {
|
||||||
|
if !enabled {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let first: u64 = capture.get(1).unwrap().as_str().parse().unwrap();
|
||||||
|
let second: u64 = capture.get(2).unwrap().as_str().parse().unwrap();
|
||||||
|
|
||||||
|
total += first * second;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
print_res!("Sum of multiplications: {total}");
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn main() -> color_eyre::Result<()> {
|
||||||
|
let context = load()?;
|
||||||
|
|
||||||
|
let start = Instant::now();
|
||||||
|
let parsed = parsing(&context.input)?;
|
||||||
|
let elapsed = humantime::format_duration(start.elapsed());
|
||||||
|
|
||||||
|
let start = Instant::now();
|
||||||
|
if context.part == 1 {
|
||||||
|
part1(parsed);
|
||||||
|
} else {
|
||||||
|
part2(parsed);
|
||||||
|
}
|
||||||
|
let elapsed_part = humantime::format_duration(start.elapsed());
|
||||||
|
|
||||||
|
println!(" Parsing: {elapsed}");
|
||||||
|
println!(" Solving: {elapsed_part}");
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue