This commit is contained in:
sepia 2024-12-07 08:06:32 -06:00
parent 8036ad4d58
commit 6960cc3a99
4 changed files with 86 additions and 1 deletions

10
Cargo.lock generated
View File

@ -22,6 +22,7 @@ name = "advent"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"dotenvy", "dotenvy",
"itertools",
"rayon", "rayon",
"regex", "regex",
"reqwest", "reqwest",
@ -586,6 +587,15 @@ version = "2.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ddc24109865250148c2e0f3d25d4f0f479571723792d3802153c60922a4fb708" checksum = "ddc24109865250148c2e0f3d25d4f0f479571723792d3802153c60922a4fb708"
[[package]]
name = "itertools"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186"
dependencies = [
"either",
]
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "1.0.14" version = "1.0.14"

View File

@ -5,6 +5,7 @@ edition = "2021"
[dependencies] [dependencies]
dotenvy = "0.15.7" dotenvy = "0.15.7"
itertools = "0.13.0"
rayon = "1.10.0" rayon = "1.10.0"
regex = "1.11.1" regex = "1.11.1"
reqwest = { version = "0.12.9", features = ["blocking"] } reqwest = { version = "0.12.9", features = ["blocking"] }

67
src/day7.rs Normal file
View File

@ -0,0 +1,67 @@
use itertools::{repeat_n, Itertools};
use rayon::prelude::*;
use regex::Regex;
pub fn day7() {
let lines: Vec<(u64, Vec<u64>)> = parse_input(&crate::input(7));
let simple_operations = vec![Operation::Add, Operation::Mul];
let simple_operations_total: u64 = lines
.par_iter()
.filter(|(goal, operands)| possible_totals(operands, &simple_operations).contains(goal))
.map(|(goal, _)| goal)
.sum();
let all_operations = vec![Operation::Add, Operation::Mul, Operation::Cat];
let all_operations_total: u64 = lines
.par_iter()
.filter(|(goal, operands)| possible_totals(operands, &all_operations).contains(goal))
.map(|(goal, _)| goal)
.sum();
println!("Total for +/*: {}", simple_operations_total);
println!("Total for All: {}", all_operations_total);
}
fn possible_totals(operands: &Vec<u64>, operations: &Vec<Operation>) -> Vec<u64> {
repeat_n(operations, operands.len() - 1)
.multi_cartesian_product()
.map(|operations| {
operands[1..]
.iter()
.zip(operations)
.fold(operands[0], |acc, (e, op)| op.apply(acc, *e))
})
.collect()
}
fn parse_input(input: &str) -> Vec<(u64, Vec<u64>)> {
input
.lines()
.map(|line| {
let all: Vec<u64> = Regex::new(r"\D+")
.unwrap()
.split(line)
.map(|s| s.parse().unwrap())
.collect();
(all[0], all[1..].to_vec())
})
.collect()
}
#[derive(Clone)]
enum Operation {
Add,
Mul,
Cat,
}
impl Operation {
fn apply(&self, a: u64, b: u64) -> u64 {
match self {
Self::Add => a + b,
Self::Mul => a * b,
Self::Cat => (a.to_string() + &b.to_string()).parse().unwrap(),
}
}
}

View File

@ -1,12 +1,19 @@
#[allow(dead_code)]
mod day1; mod day1;
#[allow(dead_code)]
mod day2; mod day2;
#[allow(dead_code)]
mod day3; mod day3;
#[allow(dead_code)]
mod day4; mod day4;
#[allow(dead_code)]
mod day5; mod day5;
#[allow(dead_code)]
mod day6; mod day6;
mod day7;
fn main() { fn main() {
day6::day6(); day7::day7();
} }
pub fn input(day: u8) -> String { pub fn input(day: u8) -> String {