generated from blizzardfinnegan/advent-of-code
Start day 2
This commit is contained in:
parent
4ff8f1f181
commit
75d6a78d23
6 changed files with 6091 additions and 13 deletions
|
@ -998,4 +998,3 @@
|
|||
59 62 64 66 68
|
||||
68 70 73 75 77
|
||||
35 33 30 27 26 25 24 23
|
||||
|
||||
|
|
5944
day02/output.txt
Normal file
5944
day02/output.txt
Normal file
File diff suppressed because it is too large
Load diff
21
day02/part2instructions.txt
Normal file
21
day02/part2instructions.txt
Normal file
|
@ -0,0 +1,21 @@
|
|||
--- Part Two ---
|
||||
|
||||
The engineers are surprised by the low number of safe reports until they realize they forgot to tell you about the Problem Dampener.
|
||||
|
||||
The Problem Dampener is a reactor-mounted module that lets the reactor safety systems tolerate a single bad level in what would otherwise be a safe report. It's like the bad level never happened!
|
||||
|
||||
Now, the same rules apply as before, except if removing a single level from an unsafe report would make it safe, the report instead counts as safe.
|
||||
|
||||
More of the above example's reports are now safe:
|
||||
|
||||
7 6 4 2 1: Safe without removing any level.
|
||||
1 2 7 8 9: Unsafe regardless of which level is removed.
|
||||
9 7 6 2 1: Unsafe regardless of which level is removed.
|
||||
1 3 2 4 5: Safe by removing the second level, 3.
|
||||
8 6 4 4 1: Safe by removing the third level, 4.
|
||||
1 3 6 7 9: Safe without removing any level.
|
||||
|
||||
Thanks to the Problem Dampener, 4 reports are actually safe!
|
||||
|
||||
Update your analysis by handling situations where the Problem Dampener can remove a single level from unsafe reports. How many reports are now safe?
|
||||
|
|
@ -1,9 +1,6 @@
|
|||
use std::env;
|
||||
use std::collections::BinaryHeap;
|
||||
use std::cmp::Reverse;
|
||||
use std::fs;
|
||||
|
||||
const FILE_NAME : &str = "data/testInput.txt";
|
||||
const FILE_NAME : &str = "data/input.txt";
|
||||
const MIN_INCREMENT : i32 = 1;
|
||||
const MAX_INCREMENT : i32 = 3;
|
||||
const MIN_DECREMENT : i32 = -1;
|
||||
|
@ -22,14 +19,45 @@ fn main(){
|
|||
});
|
||||
reports.push(vec);
|
||||
}
|
||||
for report in reports{
|
||||
if report.is_sorted_by(|a,b|a > b) | report.is_sorted_by(|a,b| a < b){
|
||||
while report.len() > 0{
|
||||
for mut report in reports{
|
||||
if report.is_sorted_by(|a,b|a > b) {
|
||||
while report.len() > 0{
|
||||
if report.len() == 1{
|
||||
final_sum += 1;
|
||||
break;
|
||||
}
|
||||
let previous = report.pop().unwrap();
|
||||
let possible_next = report.get(1);
|
||||
|
||||
let possible_next = report.get(report.len()-1);
|
||||
if let Some(next) = possible_next{
|
||||
let diff = next - previous;
|
||||
if (diff >= MIN_INCREMENT) & (diff <= MAX_INCREMENT){
|
||||
continue;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if report.is_sorted_by(|a,b| a < b){
|
||||
while report.len() > 0{
|
||||
if report.len() == 1{
|
||||
final_sum += 1;
|
||||
break;
|
||||
}
|
||||
let previous = report.pop().unwrap();
|
||||
let possible_next = report.get(report.len()-1);
|
||||
if let Some(next) = possible_next{
|
||||
let diff = next - previous;
|
||||
if (diff <= MIN_DECREMENT) & (diff >= MAX_DECREMENT){
|
||||
continue;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
//println!("Failed sort: {:?}",report);
|
||||
}
|
||||
}
|
||||
println!("{}",final_sum);
|
||||
}
|
||||
|
|
89
day02/src/bin/part2.rs
Normal file
89
day02/src/bin/part2.rs
Normal file
|
@ -0,0 +1,89 @@
|
|||
use std::fs;
|
||||
|
||||
const FILE_NAME : &str = "data/input.txt";
|
||||
const MIN_INCREMENT : i32 = 1;
|
||||
const MAX_INCREMENT : i32 = 3;
|
||||
const MIN_DECREMENT : i32 = -1;
|
||||
const MAX_DECREMENT : i32 = -3;
|
||||
|
||||
|
||||
fn main(){
|
||||
let mut final_sum:i32 = 0;
|
||||
let mut reports : Vec<Vec<i32>> = Vec::new();
|
||||
let contents = fs::read_to_string(FILE_NAME).expect("Bad path!");
|
||||
for line in contents.lines(){
|
||||
let val = line.split_whitespace().collect::<Vec<&str>>();
|
||||
let mut vec = Vec::new();
|
||||
val.iter().for_each(|i|{
|
||||
vec.push(i.parse::<i32>().unwrap());
|
||||
});
|
||||
reports.push(vec);
|
||||
}
|
||||
for report in reports {
|
||||
let first = report.get(0).unwrap();
|
||||
let second = report.get(1).unwrap();
|
||||
let increment:bool = if first-second>0 { true } else { false };
|
||||
println!("{:?}",report);
|
||||
let first_check = check_report(report,increment,true);
|
||||
final_sum += if first_check { 1 } else { 0 };
|
||||
println!("{:?}",first_check);
|
||||
println!("------");
|
||||
}
|
||||
println!("{}",final_sum);
|
||||
}
|
||||
|
||||
fn check_value(first:&i32, second:&i32, increment:bool) -> bool {
|
||||
let diff = first - second;
|
||||
//println!("{:?};{:?};{:?}",first,second,diff);
|
||||
if increment {
|
||||
return (diff >= MIN_INCREMENT) & (diff <= MAX_INCREMENT);
|
||||
} else {
|
||||
return (diff <= MIN_DECREMENT) & (diff >= MAX_DECREMENT);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
fn check_report(report:Vec<i32>,increment:bool,recurse:bool) -> bool{
|
||||
for index in 0..report.len(){
|
||||
let current = report.get(index).unwrap();
|
||||
let previous: Option<i32> =
|
||||
if index >= 1 { report.get(index-1).copied()}
|
||||
else { None };
|
||||
let next: Option<i32> =
|
||||
if index + 1 <= report.len() { report.get(index+1).copied() }
|
||||
else { None };
|
||||
|
||||
if !next.is_none(){
|
||||
let next_val = next.unwrap();
|
||||
let check_val = check_value(current,&next_val,increment);
|
||||
if !check_val{
|
||||
//println!("{:?},{:?},{},{}",current,next_val,increment,check_val);
|
||||
if recurse{
|
||||
let mut report_no_current = report.clone();
|
||||
let mut report_no_next = report.clone();
|
||||
report_no_current.remove(index);
|
||||
report_no_next.remove(index+1);
|
||||
print!("{:?};\t",report_no_current);
|
||||
let no_current = check_report(report_no_current,increment,false);
|
||||
println!("{:?}",no_current);
|
||||
print!("{:?};\t",report_no_next);
|
||||
let no_next = check_report(report_no_next,increment,false);
|
||||
println!("{:?}",no_next);
|
||||
return no_current | no_next;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
|
||||
//change between 2 points must be 1-3 (plus or minus)
|
||||
//increment/decrement must be consistent throughout the report
|
||||
// see increment variable
|
||||
//removing a _singular_ point can still create a valid report
|
||||
// check against one_fail, then check valid for previous and next instead of current
|
||||
// and next
|
||||
}
|
||||
false
|
||||
}
|
|
@ -1,3 +0,0 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue