This commit is contained in:
Rh4096 2024-12-07 13:09:48 +07:00
parent 289ae15cc5
commit a3f3964e96
3 changed files with 159 additions and 4 deletions

View file

@ -28,6 +28,7 @@ fn main() -> Result<()> {
4 => AocDayFourSolution,
5 => AocDayFiveSolution,
6 => AocDaySixSolution,
7 => AocDaySevenSolution,
};
Ok(())

View file

@ -1,8 +1,109 @@
use std::{fs::read_to_string, path::Path};
use std::{fs::read_to_string, ops::Mul, path::Path};
use super::AocSolution;
use crate::utils::Result;
fn compute(nums: &[u64], total: u64) -> bool {
let mut equations = vec![false; nums.len() - 1];
fn _inc_xf(flags: &mut [bool]) {
let mut carry = true;
for flag in flags.iter_mut().rev() {
if !carry {
break;
}
let tmp = *flag;
*flag = !tmp;
carry = tmp;
}
}
loop {
if equations.iter().zip(1..).fold(
nums[0],
|n, (&x, idx)| {
if x {
n * nums[idx]
} else {
n + nums[idx]
}
},
) == total
{
return true;
} else {
if equations.iter().all(|b| *b) {
break;
}
_inc_xf(&mut equations);
}
}
false
}
fn concat(n1: u64, n2: u64) -> u64 {
format!("{n1}{n2}").parse().unwrap()
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Operation {
Add,
Mul,
Concat,
}
impl Operation {
fn inc(self) -> Self {
match self {
Self::Add => Self::Mul,
Self::Mul => Self::Concat,
Self::Concat => Self::Add,
}
}
}
fn compute_ver_two(nums: &[u64], total: u64) -> bool {
let mut equations = vec![Operation::Add; nums.len() - 1];
fn _inc_xf(flags: &mut [Operation]) {
let mut carry = true;
for flag in flags.iter_mut().rev() {
if !carry {
break;
}
let tmp = *flag;
*flag = tmp.inc();
carry = tmp == Operation::Concat;
}
}
loop {
if equations
.iter()
.zip(1..)
.fold(nums[0], |n, (&x, idx)| match x {
Operation::Add => n + nums[idx],
Operation::Mul => n * nums[idx],
Operation::Concat => concat(n, nums[idx]),
})
== total
{
return true;
} else {
if equations.iter().all(|b| *b == Operation::Concat) {
break;
}
_inc_xf(&mut equations);
}
}
false
}
#[derive(Clone, Copy)]
pub struct AocDaySevenSolution;
impl AocSolution for AocDaySevenSolution {
@ -11,15 +112,66 @@ impl AocSolution for AocDaySevenSolution {
fn get_input(&self, path: Option<&Path>) -> Result<String> {
Ok(match path {
Some(p) => read_to_string(p)?,
None => read_to_string("./input/day_day_7.txt")?,
None => read_to_string("./input/day_7.txt")?,
})
}
fn part_one(&self, input: &str) -> Result<Self::Output> {
todo!()
let mut sum = 0;
for line in input.lines() {
if let Some((total, nums)) = line.split_once(':') {
let total = total.parse::<u64>()?;
let nums = nums
.split_whitespace()
.map(|n| n.parse::<u64>().map_err(|e| e.into()))
.collect::<Result<Vec<u64>>>()?;
if compute(&nums, total) {
sum += total;
}
}
}
Ok(sum)
}
fn part_two(&self, input: &str) -> Result<Self::Output> {
todo!()
let mut sum = 0;
for line in input.lines() {
if let Some((total, nums)) = line.split_once(':') {
let total = total.parse::<u64>()?;
let nums = nums
.split_whitespace()
.map(|n| n.parse::<u64>().map_err(|e| e.into()))
.collect::<Result<Vec<u64>>>()?;
if compute_ver_two(&nums, total) {
sum += total;
}
}
}
Ok(sum)
}
}
#[test]
fn test() {
let sol = AocDaySevenSolution;
assert_eq!(
sol.part_one(
r#"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"#
)
.unwrap(),
3749
);
}

View file

@ -8,6 +8,8 @@ pub type Result<T> = std::result::Result<T, Error>;
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("Error parsing integer: {0}")]
ParseNumErr(#[from] std::num::ParseIntError),
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("Invalid command usage: {0}")]