mirror of
https://github.com/kristoferssolo/Advent-of-Code.git
synced 2026-02-04 06:12:01 +00:00
day-07 part-1
This commit is contained in:
28
2024/day-07/Cargo.toml
Normal file
28
2024/day-07/Cargo.toml
Normal file
@@ -0,0 +1,28 @@
|
||||
[package]
|
||||
name = "day-07"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
itertools.workspace = true
|
||||
nom.workspace = true
|
||||
tracing.workspace = true
|
||||
tracing-subscriber.workspace = true
|
||||
miette.workspace = true
|
||||
thiserror.workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
divan.workspace = true
|
||||
rstest.workspace = true
|
||||
test-log.workspace = true
|
||||
|
||||
[[bench]]
|
||||
name = "day-07-bench"
|
||||
path = "benches/benchmarks.rs"
|
||||
harness = false
|
||||
|
||||
[lints.clippy]
|
||||
pedantic = "warn"
|
||||
nursery = "warn"
|
||||
21
2024/day-07/benches/benchmarks.rs
Normal file
21
2024/day-07/benches/benchmarks.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
use day_07::*;
|
||||
|
||||
fn main() {
|
||||
divan::main();
|
||||
}
|
||||
|
||||
#[divan::bench]
|
||||
fn part1() {
|
||||
part1::process(divan::black_box(include_str!(
|
||||
"../input1.txt",
|
||||
)))
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[divan::bench]
|
||||
fn part2() {
|
||||
part2::process(divan::black_box(include_str!(
|
||||
"../input2.txt",
|
||||
)))
|
||||
.unwrap();
|
||||
}
|
||||
12
2024/day-07/src/bin/part1.rs
Normal file
12
2024/day-07/src/bin/part1.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
use day_07::part1::process;
|
||||
use miette::{Context, Result};
|
||||
|
||||
#[tracing::instrument]
|
||||
fn main() -> Result<()> {
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
let file = include_str!("../../input1.txt");
|
||||
let result = process(file).context("process part 1")?;
|
||||
println!("{}", result);
|
||||
Ok(())
|
||||
}
|
||||
12
2024/day-07/src/bin/part2.rs
Normal file
12
2024/day-07/src/bin/part2.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
use day_07::part2::process;
|
||||
use miette::{Context, Result};
|
||||
|
||||
#[tracing::instrument]
|
||||
fn main() -> Result<()> {
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
let file = include_str!("../../input2.txt");
|
||||
let result = process(file).context("process part 2")?;
|
||||
println!("{}", result);
|
||||
Ok(())
|
||||
}
|
||||
2
2024/day-07/src/lib.rs
Normal file
2
2024/day-07/src/lib.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub mod part1;
|
||||
pub mod part2;
|
||||
134
2024/day-07/src/part1.rs
Normal file
134
2024/day-07/src/part1.rs
Normal file
@@ -0,0 +1,134 @@
|
||||
use std::str::FromStr;
|
||||
|
||||
use miette::{Diagnostic, Result};
|
||||
use thiserror::Error;
|
||||
use tracing::Instrument;
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
enum Operator {
|
||||
Add,
|
||||
Multiply,
|
||||
}
|
||||
|
||||
impl Operator {
|
||||
const fn apply(&self, a: usize, b: usize) -> usize {
|
||||
match self {
|
||||
Self::Add => a + b,
|
||||
Self::Multiply => a * b,
|
||||
}
|
||||
}
|
||||
|
||||
const fn all_operators() -> [Self; 2] {
|
||||
[Self::Add, Self::Multiply]
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Error, Diagnostic)]
|
||||
enum EquationError {
|
||||
#[error("Failed to parse equation")]
|
||||
ParseError,
|
||||
#[error("Missing value")]
|
||||
MissingValue,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Equation {
|
||||
result: usize,
|
||||
numbers: Vec<usize>,
|
||||
}
|
||||
|
||||
impl Equation {
|
||||
fn find_result(&self) -> Option<usize> {
|
||||
fn recursive_find(
|
||||
numbers: &[usize],
|
||||
target: usize,
|
||||
current: usize,
|
||||
index: usize,
|
||||
is_first: bool,
|
||||
) -> Option<usize> {
|
||||
if index == numbers.len() {
|
||||
return if current == target {
|
||||
Some(current)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
}
|
||||
let num = numbers[index];
|
||||
|
||||
for op in Operator::all_operators() {
|
||||
if let Some(result) = recursive_find(
|
||||
numbers,
|
||||
target,
|
||||
if is_first {
|
||||
num
|
||||
} else {
|
||||
op.apply(current, num)
|
||||
},
|
||||
index + 1,
|
||||
false,
|
||||
) {
|
||||
return Some(result);
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
if self.numbers.is_empty() {
|
||||
return None;
|
||||
}
|
||||
recursive_find(&self.numbers, self.result, 0, 0, true)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for Equation {
|
||||
type Err = EquationError;
|
||||
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
|
||||
let line = s.trim().split(':').collect::<Vec<_>>();
|
||||
let result = line
|
||||
.first()
|
||||
.ok_or(EquationError::MissingValue)?
|
||||
.parse()
|
||||
.map_err(|_| EquationError::ParseError)?;
|
||||
|
||||
let numbers = line
|
||||
.last()
|
||||
.ok_or(EquationError::MissingValue)?
|
||||
.split_whitespace()
|
||||
.map(str::parse)
|
||||
.collect::<Result<Vec<usize>, _>>()
|
||||
.map_err(|_| EquationError::ParseError)?;
|
||||
|
||||
Ok(Self { result, numbers })
|
||||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument]
|
||||
pub fn process(input: &str) -> Result<usize> {
|
||||
let result = input
|
||||
.lines()
|
||||
.map(Equation::from_str)
|
||||
.filter_map(|eq| eq.ok()?.find_result())
|
||||
.sum();
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_process() -> Result<()> {
|
||||
let input = "190: 10 19
|
||||
3267: 81 40 27
|
||||
83: 17 5
|
||||
156: 15 6
|
||||
7290: 6 8 6 15
|
||||
161011: 16 10 13
|
||||
192: 17 8 14
|
||||
21037: 9 7 18 13
|
||||
292: 11 6 16 20";
|
||||
let result = 3749;
|
||||
assert_eq!(process(input)?, result);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
21
2024/day-07/src/part2.rs
Normal file
21
2024/day-07/src/part2.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
use miette::Result;
|
||||
|
||||
#[tracing::instrument]
|
||||
pub fn process(input: &str) -> Result<usize> {
|
||||
todo!("day xx - part 2");
|
||||
Ok(0)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_process() -> Result<()> {
|
||||
let input = "";
|
||||
todo!("haven't built test yet");
|
||||
let result = 0;
|
||||
assert_eq!(process(input)?, result);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user