Skip to content

Commit

Permalink
feat: solve 02/2024 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
manhunto committed Dec 2, 2024
1 parent 24dc54f commit 0504f42
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 7 deletions.
7 changes: 4 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@

# 2024

| Day | Solved | Part 1 time (ms) | Part 2 time (ms) |
|---------------------------------------------------------------|:------:|-----------------:|-----------------:|
| [Day 1: Historian Hysteria ](src/solutions/year2024/day01.rs) | ⭐⭐ | 0.221 | 0.219 |
| Day | Solved | Part 1 time (ms) | Part 2 time (ms) |
|--------------------------------------------------------------|:------:|-----------------:|-----------------:|
| [Day 1: Historian Hysteria](src/solutions/year2024/day01.rs) | ⭐⭐ | 0.221 | 0.219 |
| [Day 2: Red-Nosed Reports](src/solutions/year2024/day02.rs) || 0.508 | |

# 2023

Expand Down
1 change: 1 addition & 0 deletions src/solutions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub fn solution(day: DayNumber, year: Year) -> Box<dyn Solution> {
match year {
Year::Year2024 => match i {
1 => Box::new(year2024::day01::Day01),
2 => Box::new(year2024::day02::Day02),
_ => panic!("Day not exist"),
},
Year::Year2023 => match i {
Expand Down
84 changes: 80 additions & 4 deletions src/solutions/year2024/day02.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,102 @@
use crate::solutions::year2024::day02::State::{Decreasing, Equals, Increasing};
use crate::solutions::Solution;

pub struct Day02;

impl Solution for Day02 {
fn part_one(&self, _input: &str) -> String {
String::new()
fn part_one(&self, input: &str) -> String {
let input = self.parse(input);

input
.iter()
.filter(|report| {
let mut state: Option<State> = None;

for i in 0..report.len() - 1 {
let first = report.get(i).unwrap();
let second = report.get(i + 1).unwrap();

if (first - second).abs() > 3 {
return false;
}

let current_state = if first < second {
Increasing
} else if first > second {
Decreasing
} else if first == second {
Equals
} else {
unreachable!()
};

if let Some(inner_state) = state {
if inner_state == Equals {
return false;
}
if inner_state != current_state {
return false;
}
} else {
state = Some(current_state);
}
}

true
})
.count()
.to_string()
}

fn part_two(&self, _input: &str) -> String {
String::new()
}
}

impl Day02 {
fn parse(&self, input: &str) -> Vec<Vec<i32>> {
input
.lines()
.map(|line| {
line.split_terminator(" ")
.map(|n| n.parse::<i32>().unwrap())
.collect::<Vec<i32>>()
})
.collect()
}
}

#[derive(Copy, Clone, PartialEq)]
enum State {
Increasing,
Decreasing,
Equals,
}

#[cfg(test)]
mod tests {
use crate::solutions::year2024::day02::Day02;
use crate::solutions::Solution;

const EXAMPLE: &str = r#""#;
const EXAMPLE: &str = r#"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"#;

#[test]
fn part_one_example_test() {
assert_eq!("", Day02.part_one(EXAMPLE));
assert_eq!("2", Day02.part_one(EXAMPLE));
}

#[test]
fn part_one_with_equals_test() {
assert_eq!("0", Day02.part_one("2 2 2"));
assert_eq!("0", Day02.part_one("2 2 1"));
assert_eq!("0", Day02.part_one("2 2 3"));
assert_eq!("0", Day02.part_one("1 2 2"));
assert_eq!("0", Day02.part_one("2 2 2"));
assert_eq!("0", Day02.part_one("3 2 2"));
}
}

0 comments on commit 0504f42

Please sign in to comment.