Compare commits

...

2 commits

Author SHA1 Message Date
b397d625c8 🚧 Day 2: Failed attempt at solving part 2 2024-12-02 07:26:48 +01:00
faec1277d3 Day 2 (part 1, part 2 working in tests) 2024-12-02 07:09:25 +01:00
3 changed files with 94 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))
[![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)
![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.

View file

@ -2,31 +2,107 @@ use super::{Answer, Day, DayImpl};
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, mut tolerance: usize) -> bool {
let mut direction = None;
let mut skip = false;
for i in 1..self.0.len() {
if skip {
skip = false;
continue;
}
if !Self::safe_comparison(self.0[i - 1], self.0[i], &mut direction) {
if tolerance > 0 && i > 1 {
tolerance -= 1;
if !Self::safe_comparison(self.0[i - 2], self.0[i], &mut direction) {
skip = true;
continue;
}
}
return false;
}
}
true
}
fn safe_comparison(a: i8, b: i8, direction: &mut Option<Direction>) -> bool {
match (b - a, &direction) {
/* Over limits */
(..=-4 | 4.., _) => return false,
/* Decide on direction */
(..=-1, None) => *direction = Some(Direction::Decreasing),
(1.., None) => *direction = Some(Direction::Increasing),
/* Wrong Direction */
(1.., Some(Direction::Decreasing)) => return false,
(..=-1, Some(Direction::Increasing)) => return false,
/* No Change */
(0, _) => return false,
_ => {}
}
return 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> {
fn init_test() -> (Self, Data) {
Self::init(include_str!("test_inputs/test02.txt"))
}
fn expected_results() -> (Answer, Answer) {
(Answer::Number(0), Answer::Number(0))
(Answer::Number(2), Answer::Number(4))
}
fn init(input: &str) -> (Self, Data) {
(
Self {},
input
.lines()
.map(|v| v.parse::<u64>().expect("error while parsing input."))
.collect(),
)
(Self {}, input.lines().map(|l| l.into()).collect())
}
fn one(&self, data: &mut Data) -> Answer {
Answer::Number(data.len() as u64)
let mut safe_count = 0;
for report in data {
if report.is_safe(0) {
safe_count += 1;
}
}
return Answer::Number(safe_count);
}
fn two(&self, data: &mut Data) -> Answer {
Answer::Number(data.len() as u64)
let mut safe_count = 0;
for report in data {
if report.is_safe(1) {
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