day02
This commit is contained in:
parent
11b2addf4b
commit
35f9cbce37
4 changed files with 1086 additions and 3 deletions
6
res/02/example
Normal file
6
res/02/example
Normal file
|
@ -0,0 +1,6 @@
|
|||
7 6 4 2 1
|
||||
1 2 7 8 9
|
||||
9 7 6 2 1
|
||||
1 3 2 4 5
|
||||
8 6 4 4 1
|
||||
1 3 6 7 9
|
1000
res/02/input
Normal file
1000
res/02/input
Normal file
File diff suppressed because it is too large
Load diff
78
src/days/d02.rs
Normal file
78
src/days/d02.rs
Normal file
|
@ -0,0 +1,78 @@
|
|||
use std::fs;
|
||||
|
||||
pub fn solve() {
|
||||
let path = "res/02/input";
|
||||
|
||||
let contents = fs::read_to_string(path).expect("Something went wrong reading the file");
|
||||
|
||||
let reports = contents
|
||||
.lines()
|
||||
.map(|line| {
|
||||
line.split(" ")
|
||||
.map(|n| n.parse::<usize>().unwrap())
|
||||
.collect::<Vec<usize>>()
|
||||
})
|
||||
.collect::<Vec<Vec<usize>>>();
|
||||
|
||||
let result = reports
|
||||
.iter()
|
||||
.filter(|r| {
|
||||
let mut curr = r[0];
|
||||
if r[1] > curr {
|
||||
for i in 1..r.len() {
|
||||
if r[i] > curr && curr.abs_diff(r[i]) < 4 {
|
||||
curr = r[i];
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for i in 1..r.len() {
|
||||
if r[i] < curr && curr.abs_diff(r[i]) < 4 {
|
||||
curr = r[i];
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
true
|
||||
})
|
||||
.count();
|
||||
|
||||
println!("Result 1: {}", result);
|
||||
|
||||
let result = reports
|
||||
.iter()
|
||||
.filter(|r| {
|
||||
if safe(r.clone().to_vec(), false) {
|
||||
return true;
|
||||
}
|
||||
let mut r2 = r.clone().to_vec();
|
||||
r2.remove(0);
|
||||
if safe(r2, true) {
|
||||
return true;
|
||||
}
|
||||
let mut r3 = r.clone().to_vec();
|
||||
r3.remove(1);
|
||||
safe(r3, true)
|
||||
})
|
||||
.count();
|
||||
|
||||
println!("Result 2: {}", result);
|
||||
}
|
||||
|
||||
fn safe(report: Vec<usize>, dampener_used: bool) -> bool {
|
||||
let mut dampener_used = dampener_used;
|
||||
let dir: isize = if report[1] > report[0] { 1 } else { -1 };
|
||||
let mut curr = report[0];
|
||||
for i in 1..report.len() {
|
||||
if report[i] as isize * dir > curr as isize * dir && curr.abs_diff(report[i]) < 4 {
|
||||
curr = report[i];
|
||||
} else if !dampener_used {
|
||||
dampener_used = true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
|
@ -4,7 +4,7 @@ extern crate scan_fmt;
|
|||
use std::time::Instant;
|
||||
|
||||
fn main() {
|
||||
days::d01::solve()
|
||||
days::d02::solve()
|
||||
//_all_days()
|
||||
}
|
||||
|
||||
|
@ -17,11 +17,10 @@ fn _all_days() {
|
|||
days::d01::solve();
|
||||
time = _print_elapsed(time);
|
||||
|
||||
/*
|
||||
println!("\nDay 2");
|
||||
days::d02::solve();
|
||||
time = _print_elapsed(time);
|
||||
|
||||
/*
|
||||
println!("\nDay 3");
|
||||
days::d03::solve();
|
||||
time = _print_elapsed(time);
|
||||
|
|
Loading…
Reference in a new issue