Day 2 (part 1, part 2 working in tests)

This commit is contained in:
LeMoonStar 2024-12-02 07:09:25 +01:00
parent 0dc9e330f7
commit faec1277d3
3 changed files with 105 additions and 12 deletions

View file

@ -4,7 +4,7 @@
[![Language: Rust](https://img.shields.io/badge/Language-Rust-orange.svg?style=flat-square)](https://en.wikipedia.org/wiki/Rust_(programming_language)) [![Language: Rust](https://img.shields.io/badge/Language-Rust-orange.svg?style=flat-square)](https://en.wikipedia.org/wiki/Rust_(programming_language))
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square)](https://mit-license.org/) [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square)](https://mit-license.org/)
![Days completed](https://img.shields.io/badge/Days%20completed-1-red?style=flat-square) ![Days completed](https://img.shields.io/badge/Days%20completed-1-red?style=flat-square)
![Stars](https://img.shields.io/badge/Stars-2-yellow?style=flat-square) ![Stars](https://img.shields.io/badge/Stars-3-yellow?style=flat-square)
> ⚠️ 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.

View file

@ -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);
} }
} }

View 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