02 (WIP)
This commit is contained in:
@@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "day_02"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
@@ -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
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,50 @@
|
|||||||
|
use std::fs::read_to_string;
|
||||||
|
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
const INPUT: &str = "example.txt";
|
||||||
|
#[cfg(not(debug_assertions))]
|
||||||
|
const INPUT: &str = "input.txt";
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut count_safe = 0;
|
||||||
|
read_to_string(INPUT).unwrap().lines().for_each(|line| {
|
||||||
|
let data: Vec<_> = line
|
||||||
|
.split_whitespace()
|
||||||
|
.map(|item| item.parse::<isize>().unwrap())
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let mut diffs = vec![];
|
||||||
|
|
||||||
|
for first in 0..data.len() - 1 {
|
||||||
|
let second = first + 1;
|
||||||
|
diffs.push(data[second] - data[first]);
|
||||||
|
}
|
||||||
|
|
||||||
|
for diff in &diffs {
|
||||||
|
if is_unsafe(*diff) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if all_same_sign(&diffs) {
|
||||||
|
count_safe += 1;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
println!("{count_safe}");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn all_same_sign(diffs: &[isize]) -> bool {
|
||||||
|
let first = diffs[0].signum();
|
||||||
|
for diff in diffs {
|
||||||
|
if diff.signum() != first {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_unsafe(mut diff: isize) -> bool {
|
||||||
|
diff = diff.abs();
|
||||||
|
!(1..=3).contains(&diff)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user