day_7: done for small numbers
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
mod fri_06;
|
||||
mod mon_02;
|
||||
mod sun_01;
|
||||
mod thu_05;
|
||||
mod tue_03;
|
||||
// mod fri_06;
|
||||
// mod mon_02;
|
||||
// mod sun_01;
|
||||
// mod thu_05;
|
||||
// mod tue_03;
|
||||
// mod wed_04;
|
||||
mod sat_07;
|
||||
mod types;
|
||||
mod utils;
|
||||
mod wed_04;
|
||||
|
||||
use types::*;
|
||||
|
||||
@@ -50,8 +51,12 @@ pub fn historian_hysteria() {
|
||||
// println!("The fixed update mid-queue-sum is {}", fixed_queue_mid_sum);
|
||||
|
||||
// Friday 06
|
||||
let (guard_position_count, loop_obstacle_count) =
|
||||
fri_06::guard_gallivant("./assets/day_6_guard_map_input");
|
||||
println!("The guard will visit {} positions", guard_position_count);
|
||||
println!("The guard would loop on {} positions", loop_obstacle_count);
|
||||
// let (guard_position_count, loop_obstacle_count) =
|
||||
// fri_06::guard_gallivant("./assets/day_6_guard_map_input");
|
||||
// println!("The guard will visit {} positions", guard_position_count);
|
||||
// println!("The guard would loop on {} positions", loop_obstacle_count);
|
||||
|
||||
// Saturday 07
|
||||
let calibration_result = sat_07::bridge_repair("./assets/day_7_calibrations_input");
|
||||
println!("The total calibration result is {}", calibration_result);
|
||||
}
|
||||
|
||||
83
src/advent_of_code/sat_07.rs
Normal file
83
src/advent_of_code/sat_07.rs
Normal file
@@ -0,0 +1,83 @@
|
||||
use primes::{PrimeSet, Sieve};
|
||||
|
||||
use super::*;
|
||||
|
||||
pub fn bridge_repair(input: &str) -> CalibrationResult {
|
||||
let equation_vec = utils::read_calibration_equations(input);
|
||||
let mut sum_result: CalibrationResult = 0;
|
||||
|
||||
for equation in equation_vec {
|
||||
if is_equation_true(&equation) {
|
||||
sum_result += equation.0;
|
||||
}
|
||||
}
|
||||
|
||||
sum_result
|
||||
}
|
||||
|
||||
pub fn is_equation_true(equation: &Calibration) -> bool {
|
||||
let operation_variants = generate_operation_variants(equation.1.len() as u32 - 1);
|
||||
|
||||
for variant in operation_variants {
|
||||
let mut local_operands = equation.1.clone();
|
||||
local_operands.reverse();
|
||||
let result = calc_operation(local_operands, variant);
|
||||
if result == equation.0 {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
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_num = 2i32.pow(count);
|
||||
|
||||
for variant_index in 0..variant_num {
|
||||
let mut new_vec: Vec<Operation> = vec![];
|
||||
for new_operand_index in 0..count {
|
||||
match variant_index 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);
|
||||
}
|
||||
|
||||
variant_list
|
||||
}
|
||||
|
||||
pub fn calc_operation(
|
||||
operands: CalibrationEquation,
|
||||
operators: Vec<Operation>,
|
||||
) -> CalibrationResult {
|
||||
if operands.len() == 2 {
|
||||
return operate(&operands[0], &operands[1], &operators[0]);
|
||||
}
|
||||
|
||||
return operate(
|
||||
&operands[0],
|
||||
&calc_operation(operands[1..].to_vec(), operators[1..].to_vec()),
|
||||
&operators[0],
|
||||
);
|
||||
}
|
||||
|
||||
pub fn operate(
|
||||
first: &CalibrationResult,
|
||||
second: &CalibrationResult,
|
||||
operation: &Operation,
|
||||
) -> CalibrationResult {
|
||||
match operation {
|
||||
Operation::SUM => {
|
||||
return first + second;
|
||||
}
|
||||
Operation::MUL => {
|
||||
return first * second;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -45,3 +45,14 @@ pub struct Guard {
|
||||
pub y: usize,
|
||||
pub dir: Direction,
|
||||
}
|
||||
|
||||
// Bridge repair
|
||||
pub type CalibrationResult = u128;
|
||||
pub type CalibrationEquation = Vec<CalibrationResult>;
|
||||
pub struct Calibration(pub CalibrationResult, pub CalibrationEquation);
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum Operation {
|
||||
SUM,
|
||||
MUL,
|
||||
}
|
||||
|
||||
@@ -125,6 +125,25 @@ pub fn read_floor_map_and_guard_input(input: &str) -> (Guard, FloorMap) {
|
||||
(the_guard, floor_map)
|
||||
}
|
||||
|
||||
pub fn read_calibration_equations(input: &str) -> Vec<Calibration> {
|
||||
let mut calibration_list: Vec<Calibration> = vec![];
|
||||
|
||||
for line in read_to_string(input).unwrap().lines() {
|
||||
if let Some((test_value, equation_values)) = line.split_once(": ") {
|
||||
let mut values: CalibrationEquation = vec![];
|
||||
for equation_value in equation_values.split(" ") {
|
||||
values.push(equation_value.parse::<CalibrationResult>().unwrap());
|
||||
}
|
||||
calibration_list.push(Calibration(
|
||||
test_value.parse::<CalibrationResult>().unwrap(),
|
||||
values,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
calibration_list
|
||||
}
|
||||
|
||||
pub fn calc_distance<T>(num_1: T, num_2: T) -> T
|
||||
where
|
||||
T: PartialOrd + Sub<Output = T>,
|
||||
|
||||
Reference in New Issue
Block a user