mirror of
https://github.com/LeMoonStar/AoC24.git
synced 2025-07-07 21:49:59 +02:00
✨ Day 2 (part 1, part 2 working in tests)
This commit is contained in:
parent
0dc9e330f7
commit
faec1277d3
3 changed files with 105 additions and 12 deletions
|
@ -4,7 +4,7 @@
|
||||||
[](https://en.wikipedia.org/wiki/Rust_(programming_language))
|
[](https://en.wikipedia.org/wiki/Rust_(programming_language))
|
||||||
[](https://mit-license.org/)
|
[](https://mit-license.org/)
|
||||||

|

|
||||||

|

|
||||||
|
|
||||||
> ⚠️ This README is copied from my previous years solution. It is not fully adopted to 2024 yet.
|
> ⚠️ This README is copied from my previous years solution. It is not fully adopted to 2024 yet.
|
||||||
|
|
||||||
|
|
109
src/days/d02.rs
109
src/days/d02.rs
|
@ -2,31 +2,118 @@ use super::{Answer, Day, DayImpl};
|
||||||
|
|
||||||
const CURRENT_DAY: u8 = 2;
|
const CURRENT_DAY: u8 = 2;
|
||||||
|
|
||||||
type Data = Vec<u64>;
|
pub enum Direction {
|
||||||
|
Increasing,
|
||||||
|
Decreasing,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct Report(Vec<i8>);
|
||||||
|
|
||||||
|
impl Report {
|
||||||
|
pub fn is_safe(&self, tolerance: usize) -> bool {
|
||||||
|
let mut direction = None;
|
||||||
|
let mut failures = 0;
|
||||||
|
let mut skip_last = false;
|
||||||
|
|
||||||
|
for i in 1..self.0.len() {
|
||||||
|
match (
|
||||||
|
self.0[i]
|
||||||
|
- self.0[i - if skip_last {
|
||||||
|
skip_last = false;
|
||||||
|
2
|
||||||
|
} else {
|
||||||
|
1
|
||||||
|
}],
|
||||||
|
&direction,
|
||||||
|
) {
|
||||||
|
/* Over limits */
|
||||||
|
(..=-4 | 4.., _) => {
|
||||||
|
failures += 1;
|
||||||
|
skip_last = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Decide on direction */
|
||||||
|
(..=-1, None) => direction = Some(Direction::Decreasing),
|
||||||
|
(1.., None) => direction = Some(Direction::Increasing),
|
||||||
|
|
||||||
|
/* Wrong Direction */
|
||||||
|
(1.., Some(Direction::Decreasing)) => {
|
||||||
|
failures += 1;
|
||||||
|
skip_last = true;
|
||||||
|
}
|
||||||
|
(..=-1, Some(Direction::Increasing)) => {
|
||||||
|
failures += 1;
|
||||||
|
skip_last = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* No Change */
|
||||||
|
(0, _) => {
|
||||||
|
failures += 1;
|
||||||
|
skip_last = true;
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
|
||||||
|
if failures > tolerance {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if skip_last {
|
||||||
|
println!("SKIPPING {}", i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<&str> for Report {
|
||||||
|
fn from(value: &str) -> Self {
|
||||||
|
Self(
|
||||||
|
value
|
||||||
|
.split_whitespace()
|
||||||
|
.map(|v| v.parse::<i8>().expect("error while parsing input."))
|
||||||
|
.collect(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Data = Vec<Report>;
|
||||||
impl DayImpl<Data> for Day<CURRENT_DAY> {
|
impl DayImpl<Data> for Day<CURRENT_DAY> {
|
||||||
fn init_test() -> (Self, Data) {
|
fn init_test() -> (Self, Data) {
|
||||||
Self::init(include_str!("test_inputs/test02.txt"))
|
Self::init(include_str!("test_inputs/test02.txt"))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn expected_results() -> (Answer, Answer) {
|
fn expected_results() -> (Answer, Answer) {
|
||||||
(Answer::Number(0), Answer::Number(0))
|
(Answer::Number(2), Answer::Number(4))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn init(input: &str) -> (Self, Data) {
|
fn init(input: &str) -> (Self, Data) {
|
||||||
(
|
(Self {}, input.lines().map(|l| l.into()).collect())
|
||||||
Self {},
|
|
||||||
input
|
|
||||||
.lines()
|
|
||||||
.map(|v| v.parse::<u64>().expect("error while parsing input."))
|
|
||||||
.collect(),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn one(&self, data: &mut Data) -> Answer {
|
fn one(&self, data: &mut Data) -> Answer {
|
||||||
Answer::Number(data.len() as u64)
|
let mut safe_count = 0;
|
||||||
|
for report in data {
|
||||||
|
println!("{:?}", report);
|
||||||
|
if report.is_safe(0) {
|
||||||
|
safe_count += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Answer::Number(safe_count);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn two(&self, data: &mut Data) -> Answer {
|
fn two(&self, data: &mut Data) -> Answer {
|
||||||
Answer::Number(data.len() as u64)
|
let mut safe_count = 0;
|
||||||
|
for report in data {
|
||||||
|
println!("{:?}", report);
|
||||||
|
if report.is_safe(1) {
|
||||||
|
println!("Safe!");
|
||||||
|
safe_count += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Answer::Number(safe_count);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
Loading…
Add table
Add a link
Reference in a new issue