day_7: finally done by fixing combination generator
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
use primes::{PrimeSet, Sieve};
|
||||
|
||||
use super::*;
|
||||
|
||||
pub fn bridge_repair(input: &str) -> CalibrationResult {
|
||||
@@ -12,17 +10,11 @@ pub fn bridge_repair(input: &str) -> CalibrationResult {
|
||||
}
|
||||
}
|
||||
|
||||
println!("TEST");
|
||||
for v in generate_operation_variants(3) {
|
||||
println!("{:?}", v);
|
||||
}
|
||||
println!("====");
|
||||
|
||||
sum_result
|
||||
}
|
||||
|
||||
pub fn is_equation_true(equation: &Calibration) -> bool {
|
||||
let operation_variants = generate_operation_variants(equation.1.len() as u32 - 1);
|
||||
let operation_variants = generate_operation_variants(equation.1.len() - 1);
|
||||
|
||||
for variant in operation_variants {
|
||||
let mut local_operands = equation.1.clone();
|
||||
@@ -36,58 +28,29 @@ pub fn is_equation_true(equation: &Calibration) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
// FIXME: this goes wrong on large numbers
|
||||
pub fn generate_operation_variants(count: u32) -> Vec<Vec<Operation>> {
|
||||
let mut variant_list: Vec<Vec<Operation>> = vec![];
|
||||
|
||||
let mut pset = Sieve::new(); // get prime numbers
|
||||
|
||||
let variant_max = 2i32.pow(count);
|
||||
|
||||
let square_free_list = generate_n_square_free_numbers(variant_max as usize);
|
||||
|
||||
for square_free_n in square_free_list {
|
||||
let mut new_vec: Vec<Operation> = vec![];
|
||||
for new_operand_index in 0..count {
|
||||
match square_free_n as u64 % pset.get(new_operand_index as usize) {
|
||||
0 => new_vec.push(Operation::MUL),
|
||||
_ => new_vec.push(Operation::SUM),
|
||||
}
|
||||
}
|
||||
variant_list.push(new_vec);
|
||||
}
|
||||
pub fn generate_operation_variants(count: usize) -> Vec<Vec<Operation>> {
|
||||
let variants = vec![Operation::MUL, Operation::SUM];
|
||||
let variant_list: Vec<Vec<Operation>> = generate_combinations(&variants, count);
|
||||
|
||||
variant_list
|
||||
}
|
||||
|
||||
pub fn generate_n_square_free_numbers(n: usize) -> Vec<u128> {
|
||||
let mut list: Vec<u128> = vec![];
|
||||
|
||||
let mut current_n: u128 = 1234;
|
||||
loop {
|
||||
if list.len() == n {
|
||||
break;
|
||||
}
|
||||
|
||||
if is_square_free(current_n) {
|
||||
list.push(current_n);
|
||||
}
|
||||
|
||||
current_n += 1;
|
||||
fn generate_combinations(values: &[Operation], length: usize) -> Vec<Vec<Operation>> {
|
||||
if length == 0 {
|
||||
return vec![vec![]];
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
let mut result = vec![];
|
||||
|
||||
pub fn is_square_free(number: u128) -> bool {
|
||||
let mut i = 2;
|
||||
while i * i <= number {
|
||||
if number % (i * i) == 0 {
|
||||
return false;
|
||||
for &value in values {
|
||||
let smaller_combinations = generate_combinations(values, length - 1);
|
||||
for mut smaller in smaller_combinations {
|
||||
smaller.push(value);
|
||||
result.push(smaller);
|
||||
}
|
||||
i += 1
|
||||
}
|
||||
return true;
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
pub fn calc_operation(
|
||||
@@ -119,3 +82,34 @@ pub fn operate(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Old strategy, big fail
|
||||
// pub fn generate_n_square_free_numbers(n: usize) -> Vec<u128> {
|
||||
// let mut list: Vec<u128> = vec![];
|
||||
|
||||
// let mut current_n: u128 = 1234;
|
||||
// loop {
|
||||
// if list.len() == n {
|
||||
// break;
|
||||
// }
|
||||
|
||||
// if is_square_free(current_n) {
|
||||
// list.push(current_n);
|
||||
// }
|
||||
|
||||
// current_n += 1;
|
||||
// }
|
||||
|
||||
// return list;
|
||||
// }
|
||||
|
||||
// pub fn is_square_free(number: u128) -> bool {
|
||||
// let mut i = 2;
|
||||
// while i * i <= number {
|
||||
// if number % (i * i) == 0 {
|
||||
// return false;
|
||||
// }
|
||||
// i += 1
|
||||
// }
|
||||
// return true;
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user