day 2: reports problem dampener (wrong)
This commit is contained in:
@@ -9,36 +9,12 @@ pub fn check_reports_safety(input: &str) -> ReportSafety {
|
|||||||
|
|
||||||
// 1. All levels must be all increasing or decreasing
|
// 1. All levels must be all increasing or decreasing
|
||||||
// 2. All levels must change by at leat one and at most three
|
// 2. All levels must change by at leat one and at most three
|
||||||
|
|
||||||
for report in report_list {
|
for report in report_list {
|
||||||
if report.len() < 2 {
|
if report.len() < 2 {
|
||||||
safe_count += 1;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut safe = true;
|
if is_report_safe(&report, true) {
|
||||||
let initial_direction: ReportDirection = get_report_direction(&report[0..=1]);
|
|
||||||
let mut problems_damped_count = 0;
|
|
||||||
|
|
||||||
'report_check: for index in 1..report.len() {
|
|
||||||
let prev = index - 1;
|
|
||||||
let distance = calc_distance(report[index], report[prev]);
|
|
||||||
let direction = get_report_direction(&[report[prev], report[index]]);
|
|
||||||
|
|
||||||
if report[index] == report[prev]
|
|
||||||
|| distance < 1
|
|
||||||
|| distance > 3
|
|
||||||
|| direction != initial_direction
|
|
||||||
{
|
|
||||||
if report_problem_dampener(&report) == false {
|
|
||||||
safe = false;
|
|
||||||
break 'report_check;
|
|
||||||
}
|
|
||||||
problems_damped_count += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if safe {
|
|
||||||
safe_count += 1;
|
safe_count += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -46,11 +22,45 @@ pub fn check_reports_safety(input: &str) -> ReportSafety {
|
|||||||
safe_count
|
safe_count
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: the simples solition is by brute force...
|
fn is_report_safe(report: &Report, apply_problem_dampener: bool) -> bool {
|
||||||
fn report_problem_dampener(report: &Vec<Report>) -> bool {}
|
if report.len() < 2 {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
let mut safe = true;
|
||||||
|
let initial_direction: ReportDirection = get_report_direction(&report[0..=1]);
|
||||||
|
|
||||||
|
'report_check: for index in 1..report.len() {
|
||||||
|
if !is_level_pair_valid(&report[index - 1], &report[index], &initial_direction) {
|
||||||
|
if apply_problem_dampener {
|
||||||
|
let sliced_index_report =
|
||||||
|
[&report[0..index], &report[index + 1..report.len()]].concat();
|
||||||
|
if is_report_safe(&sliced_index_report, false) {
|
||||||
|
break 'report_check;
|
||||||
|
} else {
|
||||||
|
let sliced_prev_report =
|
||||||
|
[&report[0..index - 1], &report[index..report.len()]].concat();
|
||||||
|
if is_report_safe(&sliced_prev_report, false) {
|
||||||
|
break 'report_check;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
safe = false;
|
||||||
|
break 'report_check;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
safe
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_level_pair_valid(first: &Level, second: &Level, initial_direction: &ReportDirection) -> bool {
|
||||||
|
let distance = calc_distance(*second, *first);
|
||||||
|
let direction = get_report_direction(&[*first, *second]);
|
||||||
|
|
||||||
|
!(first == second || distance < 1 || distance > 3 || direction != *initial_direction)
|
||||||
|
}
|
||||||
|
|
||||||
// FIXME: this is not a good function, since may try to access an invalid index
|
// FIXME: this is not a good function, since may try to access an invalid index
|
||||||
fn get_report_direction(report: &[Report]) -> ReportDirection {
|
fn get_report_direction(report: &[Level]) -> ReportDirection {
|
||||||
if report[1] - report[0] > 0 {
|
if report[1] - report[0] > 0 {
|
||||||
ReportDirection::Increasing
|
ReportDirection::Increasing
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -2,7 +2,8 @@ pub type Id = i32;
|
|||||||
pub type Key = Id;
|
pub type Key = Id;
|
||||||
pub type Similarity = Id;
|
pub type Similarity = Id;
|
||||||
|
|
||||||
pub type Report = i32;
|
pub type Level = i32;
|
||||||
|
pub type Report = Vec<Level>;
|
||||||
pub type ReportSafety = u32;
|
pub type ReportSafety = u32;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
|
|||||||
@@ -17,14 +17,14 @@ pub fn read_id_lists(input: &str) -> (Vec<Id>, Vec<Id>) {
|
|||||||
return (list_1, list_2);
|
return (list_1, list_2);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_report_list(input: &str) -> Vec<Vec<Report>> {
|
pub fn read_report_list(input: &str) -> Vec<Report> {
|
||||||
let mut report_list: Vec<Vec<Report>> = vec![];
|
let mut report_list: Vec<Report> = vec![];
|
||||||
|
|
||||||
for report in read_to_string(input).unwrap().lines() {
|
for report in read_to_string(input).unwrap().lines() {
|
||||||
let level_list = report.split(" ");
|
let level_list = report.split(" ");
|
||||||
let mut report_vec: Vec<Report> = vec![];
|
let mut report_vec: Report = vec![];
|
||||||
for level in level_list {
|
for level in level_list {
|
||||||
report_vec.push(level.parse::<Report>().unwrap());
|
report_vec.push(level.parse::<Level>().unwrap());
|
||||||
}
|
}
|
||||||
report_list.push(report_vec);
|
report_list.push(report_vec);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user