mirror of
https://github.com/LeMoonStar/AoC24.git
synced 2025-07-07 17:15:30 +02:00
✨ Day 3
This commit is contained in:
parent
3b942efd71
commit
37cf778b8c
5 changed files with 110 additions and 13 deletions
39
Cargo.lock
generated
39
Cargo.lock
generated
|
@ -17,6 +17,15 @@ version = "2.0.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627"
|
checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627"
|
||||||
|
|
||||||
|
[[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 = "ansi_term"
|
name = "ansi_term"
|
||||||
version = "0.12.1"
|
version = "0.12.1"
|
||||||
|
@ -43,6 +52,7 @@ dependencies = [
|
||||||
"colored",
|
"colored",
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
"mut_static",
|
"mut_static",
|
||||||
|
"regex",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -837,6 +847,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 = "reqwest"
|
name = "reqwest"
|
||||||
version = "0.11.27"
|
version = "0.11.27"
|
||||||
|
|
|
@ -12,6 +12,7 @@ aoc-macro = {path="aoc-macro"}
|
||||||
reqwest = { version = "0.11", features=["cookies", "blocking"] }
|
reqwest = { version = "0.11", features=["cookies", "blocking"] }
|
||||||
mut_static="5.0"
|
mut_static="5.0"
|
||||||
lazy_static="1.4"
|
lazy_static="1.4"
|
||||||
|
regex = "1.11"
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
opt-level = 3
|
opt-level = 3
|
||||||
|
|
|
@ -3,8 +3,8 @@
|
||||||
[](https://adventofcode.com/2024/about)
|
[](https://adventofcode.com/2024/about)
|
||||||
[](https://en.wikipedia.org/wiki/Rust_(programming_language))
|
[](https://en.wikipedia.org/wiki/Rust_(programming_language))
|
||||||
[](https://mit-license.org/)
|
[](https://mit-license.org/)
|
||||||

|

|
||||||

|

|
||||||
|
|
||||||
> ⚠️ This README is copied from my previous years solution. It is not fully adopted to 2024 yet.
|
> ⚠️ This README is copied from my previous years solution. It is not fully adopted to 2024 yet.
|
||||||
|
|
||||||
|
|
|
@ -1,32 +1,88 @@
|
||||||
use super::{Answer, Day, DayImpl};
|
use super::{Answer, Day, DayImpl};
|
||||||
|
use regex::Regex;
|
||||||
|
|
||||||
const CURRENT_DAY: u8 = 3;
|
const CURRENT_DAY: u8 = 3;
|
||||||
|
|
||||||
type Data = Vec<u64>;
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct Command {
|
||||||
|
command: String,
|
||||||
|
parameters: Vec<u64>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Command {
|
||||||
|
pub fn run(&self) -> u64 {
|
||||||
|
if !self.command.ends_with("mul") || self.parameters.len() < 2 {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
self.parameters[0] * self.parameters[1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct Program(Vec<Command>);
|
||||||
|
|
||||||
|
impl Program {
|
||||||
|
pub fn do_all_multiplications(&self) -> u64 {
|
||||||
|
self.0.iter().map(|v| v.run()).sum()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run(&self) -> u64 {
|
||||||
|
let mut enabled = true;
|
||||||
|
|
||||||
|
self.0
|
||||||
|
.iter()
|
||||||
|
.map(|v| {
|
||||||
|
if v.command.ends_with("don't") {
|
||||||
|
enabled = false;
|
||||||
|
} else if v.command.ends_with("do") {
|
||||||
|
enabled = true;
|
||||||
|
} else if enabled {
|
||||||
|
return v.run();
|
||||||
|
}
|
||||||
|
0
|
||||||
|
})
|
||||||
|
.sum()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<&str> for Program {
|
||||||
|
fn from(value: &str) -> Self {
|
||||||
|
let pattern: Regex = Regex::new(r"([a-z_']+)\(((?:\d+,?)*)\)").unwrap();
|
||||||
|
|
||||||
|
let mut commands = vec![];
|
||||||
|
for (_, [command, parameters]) in pattern.captures_iter(value).map(|c| c.extract()) {
|
||||||
|
commands.push(Command {
|
||||||
|
command: command.to_string(),
|
||||||
|
parameters: parameters
|
||||||
|
.split(",")
|
||||||
|
.map(|v| v.parse().unwrap_or(0))
|
||||||
|
.collect(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Self(commands)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Data = Program;
|
||||||
impl DayImpl<Data> for Day<CURRENT_DAY> {
|
impl DayImpl<Data> for Day<CURRENT_DAY> {
|
||||||
fn init_test() -> (Self, Data) {
|
fn init_test() -> (Self, Data) {
|
||||||
Self::init(include_str!("test_inputs/test03.txt"))
|
Self::init(include_str!("test_inputs/test03.txt"))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn expected_results() -> (Answer, Answer) {
|
fn expected_results() -> (Answer, Answer) {
|
||||||
(Answer::Number(0), Answer::Number(0))
|
(Answer::Number(161), Answer::Number(0))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn init(input: &str) -> (Self, Data) {
|
fn init(input: &str) -> (Self, Data) {
|
||||||
(
|
(Self {}, input.into())
|
||||||
Self {},
|
|
||||||
input
|
|
||||||
.lines()
|
|
||||||
.map(|v| v.parse::<u64>().expect("error while parsing input."))
|
|
||||||
.collect(),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn one(&self, data: &mut Data) -> Answer {
|
fn one(&self, data: &mut Data) -> Answer {
|
||||||
Answer::Number(data.len() as u64)
|
Answer::Number(data.do_all_multiplications())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn two(&self, data: &mut Data) -> Answer {
|
fn two(&self, data: &mut Data) -> Answer {
|
||||||
Answer::Number(data.len() as u64)
|
Answer::Number(data.run())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))
|
Loading…
Add table
Add a link
Reference in a new issue