diff --git a/2024/days/07/part1/Cargo.toml b/2024/days/07/part1/Cargo.toml new file mode 100644 index 0000000..d85b608 --- /dev/null +++ b/2024/days/07/part1/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "part1" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/2024/days/07/part1/src/main.rs b/2024/days/07/part1/src/main.rs new file mode 100644 index 0000000..8d8790a --- /dev/null +++ b/2024/days/07/part1/src/main.rs @@ -0,0 +1,41 @@ + +fn can_be_evaluated_to_aux(operands: &Vec, target: u64, current: u64, index: usize) -> bool { + if index == 0 { + return can_be_evaluated_to_aux(operands, target, operands[0], index + 1); + } else if index == operands.len() { + if current == target { + return true; + } else { + return false; + } + } else { + return can_be_evaluated_to_aux(operands, target, current * operands[index], index + 1) || can_be_evaluated_to_aux(operands, target, current + operands[index], index + 1); + } +} + +fn can_be_evaluated_to(operands: &Vec, target: u64) -> bool { + return can_be_evaluated_to_aux(operands, target, 0, 0); +} + +fn main() -> std::io::Result<()> { + let mut total: u64 = 0; + for line in std::io::stdin().lines() { + let line = line.unwrap(); + if line != "".to_string() { + let mut parts = line.split(":"); + let target = parts.next().unwrap(); + let target = target.parse::().unwrap(); + let operands: Vec = parts.next() + .unwrap() + .split(" ") + .skip(1) + .map(|e| e.parse::().unwrap()) + .collect(); + if can_be_evaluated_to(&operands, target) { + total += target; + } + } + } + println!("{}", total); + Ok(()) +}