mirror of
https://github.com/SebastianStork/advent-of-code.git
synced 2026-01-21 13:21:34 +01:00
Solve 2024 day 2 in rust
Co-Authored-By: NebelToast <>
This commit is contained in:
parent
fcff9f95a3
commit
0099ac0585
4 changed files with 1118 additions and 0 deletions
7
2024/rust/day-02/Cargo.lock
generated
Normal file
7
2024/rust/day-02/Cargo.lock
generated
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "day-02"
|
||||||
|
version = "0.1.0"
|
||||||
6
2024/rust/day-02/Cargo.toml
Normal file
6
2024/rust/day-02/Cargo.toml
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
[package]
|
||||||
|
name = "day-02"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
1000
2024/rust/day-02/input
Normal file
1000
2024/rust/day-02/input
Normal file
File diff suppressed because it is too large
Load diff
105
2024/rust/day-02/src/main.rs
Normal file
105
2024/rust/day-02/src/main.rs
Normal file
|
|
@ -0,0 +1,105 @@
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let reports = read_input();
|
||||||
|
|
||||||
|
let helper: Vec<bool> = reports
|
||||||
|
.iter()
|
||||||
|
.map(|report| check_safety(report, false))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
dbg!(helper);
|
||||||
|
|
||||||
|
let safety_score: u16 = reports
|
||||||
|
.iter()
|
||||||
|
.map(|report| check_safety(report, false))
|
||||||
|
.map(|is_safe| is_safe as u16)
|
||||||
|
.sum();
|
||||||
|
|
||||||
|
dbg!(safety_score);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_input() -> Vec<Vec<u8>> {
|
||||||
|
let contents = fs::read_to_string("./input").unwrap();
|
||||||
|
|
||||||
|
let line_to_levels = |line: &str| {
|
||||||
|
line.split(" ")
|
||||||
|
.map(|level| level.parse::<u8>().unwrap())
|
||||||
|
.collect()
|
||||||
|
};
|
||||||
|
|
||||||
|
contents.lines().map(line_to_levels).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check_safety(report: &Vec<u8>, is_dampened: bool) -> bool {
|
||||||
|
let result =
|
||||||
|
is_strictly_increasing(report, is_dampened) || is_strictly_decreasing(report, is_dampened);
|
||||||
|
if is_dampened {
|
||||||
|
//dbg!(report, result);
|
||||||
|
}
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
fn try_dampening(index: usize, report: &Vec<u8>) -> bool {
|
||||||
|
let mut temp_report = report.clone();
|
||||||
|
temp_report.remove(index);
|
||||||
|
if check_safety(&temp_report, true) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut temp_report = report.clone();
|
||||||
|
temp_report.remove(index - 1);
|
||||||
|
if check_safety(&temp_report, true) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if index + 1 < report.len() {
|
||||||
|
let mut temp_report = report.clone();
|
||||||
|
temp_report.remove(index + 1);
|
||||||
|
return check_safety(&temp_report, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_strictly_increasing(report: &Vec<u8>, is_dampened: bool) -> bool {
|
||||||
|
for i in 1..report.len() {
|
||||||
|
if report[i - 1] >= report[i] {
|
||||||
|
if is_dampened {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return try_dampening(i, report);
|
||||||
|
}
|
||||||
|
|
||||||
|
let distance = report[i].abs_diff(report[i - 1]);
|
||||||
|
if distance > 3 {
|
||||||
|
if is_dampened {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return try_dampening(i, report);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_strictly_decreasing(report: &Vec<u8>, is_dampened: bool) -> bool {
|
||||||
|
for i in 1..report.len() {
|
||||||
|
if report[i - 1] <= report[i] {
|
||||||
|
if is_dampened {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return try_dampening(i, report);
|
||||||
|
}
|
||||||
|
|
||||||
|
let distance = report[i].abs_diff(report[i - 1]);
|
||||||
|
if distance > 3 {
|
||||||
|
if is_dampened {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return try_dampening(i, report);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
true
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue