feat(day3): init
This commit is contained in:
parent
f730b2225c
commit
06dc4402d1
4 changed files with 102 additions and 1 deletions
45
Cargo.lock
generated
45
Cargo.lock
generated
|
@ -15,9 +15,19 @@ dependencies = [
|
||||||
"dotenvy",
|
"dotenvy",
|
||||||
"indoc",
|
"indoc",
|
||||||
"itertools",
|
"itertools",
|
||||||
|
"regex",
|
||||||
"ureq",
|
"ureq",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[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 = "base64"
|
name = "base64"
|
||||||
version = "0.22.1"
|
version = "0.22.1"
|
||||||
|
@ -273,6 +283,12 @@ version = "0.4.22"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
|
checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "memchr"
|
||||||
|
version = "2.7.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "miniz_oxide"
|
name = "miniz_oxide"
|
||||||
version = "0.8.0"
|
version = "0.8.0"
|
||||||
|
@ -312,6 +328,35 @@ 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]]
|
||||||
|
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]]
|
[[package]]
|
||||||
name = "ring"
|
name = "ring"
|
||||||
version = "0.17.8"
|
version = "0.17.8"
|
||||||
|
|
|
@ -7,4 +7,5 @@ edition = "2021"
|
||||||
dotenvy = "0.15.7"
|
dotenvy = "0.15.7"
|
||||||
indoc = "2.0.5"
|
indoc = "2.0.5"
|
||||||
itertools = "0.13.0"
|
itertools = "0.13.0"
|
||||||
|
regex = "1.11.1"
|
||||||
ureq = "2.11.0"
|
ureq = "2.11.0"
|
||||||
|
|
54
src/solutions/day_03.rs
Normal file
54
src/solutions/day_03.rs
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
use itertools::Itertools;
|
||||||
|
use regex::Regex;
|
||||||
|
|
||||||
|
use crate::common::{Answer, Solution};
|
||||||
|
|
||||||
|
pub struct Day03;
|
||||||
|
|
||||||
|
impl Solution for Day03 {
|
||||||
|
fn name(&self) -> &'static str {
|
||||||
|
"Mull It Over"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part_a(&self, input: &str) -> Answer {
|
||||||
|
let re = Regex::new(r"mul\((\d+),(\d+)\)").unwrap();
|
||||||
|
|
||||||
|
let sum = re
|
||||||
|
.captures_iter(input)
|
||||||
|
.map(|c| c.extract())
|
||||||
|
.map(|(_, [a, b])| a.parse::<u64>().unwrap() * b.parse::<u64>().unwrap())
|
||||||
|
.sum::<u64>();
|
||||||
|
|
||||||
|
Answer::Number(sum)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part_b(&self, input: &str) -> Answer {
|
||||||
|
let without_donts = input
|
||||||
|
.split("do()")
|
||||||
|
.map(|s| s.split_once("don't()").map(|(b, _)| b).unwrap_or(s))
|
||||||
|
.join("");
|
||||||
|
|
||||||
|
self.part_a(&without_donts)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::Day03;
|
||||||
|
use crate::common::Solution;
|
||||||
|
|
||||||
|
const INPUT_A: &str = "xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))";
|
||||||
|
|
||||||
|
const INPUT_B: &str =
|
||||||
|
"xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))";
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn part_a() {
|
||||||
|
assert_eq!(Day03.part_a(INPUT_A), 161.into());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn part_b() {
|
||||||
|
assert_eq!(Day03.part_b(INPUT_B), 48.into());
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,5 +2,6 @@ use crate::common::Solution;
|
||||||
|
|
||||||
mod day_01;
|
mod day_01;
|
||||||
mod day_02;
|
mod day_02;
|
||||||
|
mod day_03;
|
||||||
|
|
||||||
pub const SOLUTIONS: &[&dyn Solution] = &[&day_01::Day01, &day_02::Day02];
|
pub const SOLUTIONS: &[&dyn Solution] = &[&day_01::Day01, &day_02::Day02, &day_03::Day03];
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue