2025-06-06 10:51:04 -05:00
|
|
|
// https://adventofcode.com/2024/day/7
|
|
|
|
|
//
|
|
|
|
|
// Run command: cargo run ./input.txt
|
|
|
|
|
|
|
|
|
|
use std::{
|
2025-06-16 10:31:36 -05:00
|
|
|
collections::HashSet,
|
2025-06-06 10:51:04 -05:00
|
|
|
env,
|
|
|
|
|
error::Error,
|
|
|
|
|
fs::File,
|
|
|
|
|
io::{BufReader, prelude::*},
|
|
|
|
|
};
|
|
|
|
|
|
2025-06-16 10:31:36 -05:00
|
|
|
fn create_operator_set_recurse(
|
|
|
|
|
operators_permutation_set: &mut HashSet<Vec<bool>>,
|
|
|
|
|
operators: Vec<bool>,
|
|
|
|
|
) {
|
|
|
|
|
operators_permutation_set.insert(operators.clone());
|
|
|
|
|
if !operators.contains(&false) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i in 0..operators.len() {
|
|
|
|
|
if operators[i] {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
let mut new_operators = operators.clone();
|
|
|
|
|
new_operators[i] = true;
|
|
|
|
|
create_operator_set_recurse(operators_permutation_set, new_operators);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn create_operator_set(num_of_operators: usize) -> HashSet<Vec<bool>> {
|
|
|
|
|
let mut operator_sets: HashSet<Vec<bool>> = HashSet::new();
|
|
|
|
|
create_operator_set_recurse(&mut operator_sets, vec![false; num_of_operators]);
|
|
|
|
|
operator_sets
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
2025-06-06 10:51:04 -05:00
|
|
|
enum Ops {
|
|
|
|
|
Multiply,
|
|
|
|
|
Add,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
struct Equation {
|
|
|
|
|
answer: i32,
|
|
|
|
|
components: Vec<i32>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Equation {
|
|
|
|
|
fn new(equation_str: &str) -> Result<Equation, String> {
|
|
|
|
|
let split_equation: Vec<_> = equation_str.split(":").collect();
|
|
|
|
|
if split_equation.len() != 2 {
|
|
|
|
|
return Err(format!("Invalid equation string: {}", equation_str));
|
|
|
|
|
}
|
|
|
|
|
let answer = match split_equation[0].parse() {
|
|
|
|
|
Ok(a) => a,
|
|
|
|
|
Err(_) => {
|
|
|
|
|
return Err(format!(
|
|
|
|
|
"Error parsing answer {} in equation: {}",
|
|
|
|
|
split_equation[0], equation_str
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
let components = split_equation[1]
|
|
|
|
|
.split_whitespace()
|
|
|
|
|
.map(|c| match c.parse::<i32>() {
|
|
|
|
|
Ok(a) => Ok(a),
|
|
|
|
|
Err(_) => Err(format!(
|
|
|
|
|
"Error parsing component {} in equation: {}",
|
|
|
|
|
c, equation_str
|
|
|
|
|
)),
|
|
|
|
|
})
|
|
|
|
|
.collect::<Result<Vec<i32>, String>>()?;
|
|
|
|
|
Ok(Equation { answer, components })
|
|
|
|
|
}
|
2025-06-16 10:31:36 -05:00
|
|
|
|
|
|
|
|
fn solvable(&self) -> bool {
|
|
|
|
|
let op_combination: HashSet<HashSet<i32>> = HashSet::new();
|
|
|
|
|
false
|
|
|
|
|
}
|
2025-06-06 10:51:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
|
|
|
|
// Handle command input
|
|
|
|
|
let args: Vec<String> = env::args().collect();
|
|
|
|
|
if args.len() != 2 {
|
|
|
|
|
panic!(
|
|
|
|
|
"{} must be run with a single argument of file name!",
|
|
|
|
|
&args[0]
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
let input_file = &args[1];
|
|
|
|
|
|
|
|
|
|
let file = File::open(input_file)?;
|
|
|
|
|
let buffer = BufReader::new(file);
|
|
|
|
|
|
|
|
|
|
let mut equations = Vec::new();
|
|
|
|
|
for line in buffer.lines() {
|
|
|
|
|
let line = line?;
|
|
|
|
|
equations.push(Equation::new(&line)?)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
println!("{:?}", equations);
|
2025-06-16 10:31:36 -05:00
|
|
|
|
|
|
|
|
let operator_set = create_operator_set(3);
|
|
|
|
|
println!("{:?}", operator_set);
|
|
|
|
|
|
2025-06-06 10:51:04 -05:00
|
|
|
Ok(())
|
2025-06-04 20:27:32 -05:00
|
|
|
}
|