🎉」 init(2025): day 1

This commit is contained in:
2025-12-01 11:25:06 +01:00
commit 6306ab23d1
52 changed files with 2012 additions and 0 deletions

34
2024/day02/part1.rs Normal file
View File

@@ -0,0 +1,34 @@
use std::fs;
fn is_ordered(numbers: &[i32]) -> bool
{
let is_increasing = numbers.windows(2).all(|w| w[0] < w[1]);
let is_decreasing = numbers.windows(2).all(|w| w[0] > w[1]);
is_increasing || is_decreasing
}
fn check_range(numbers: &[i32]) -> bool
{
numbers.windows(2).all(|w| (1..=3).contains(&(w[1] - w[0]).abs()))
}
fn main() {
let content = fs::read_to_string("input.txt")
.expect("error with input file.");
let mut result = 0;
for lines in content.split('\n')
{
let values: Vec<i32> = lines.split_whitespace().map(|x| x.parse().expect("Invalid number")).collect();
if is_ordered(&values) && check_range(&values)
{
result += 1;
}
}
println!("{}", result);
}