Skip to content
This repository has been archived by the owner on Dec 25, 2024. It is now read-only.

Commit

Permalink
Clean up day 02
Browse files Browse the repository at this point in the history
  • Loading branch information
yamgent committed Dec 2, 2024
1 parent 373f36c commit 2ebabaa
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions src/bin/day02/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
const ACTUAL_INPUT: &str = include_str!("../../../actual_inputs/2024/02/input.txt");

fn parse_input(input: &str) -> Vec<Vec<i32>> {
input
.trim()
.lines()
.map(|line| {
line.split_whitespace()
.map(|val| val.parse::<i32>().expect("a number"))
.collect::<Vec<_>>()
})
.collect()
}

fn is_safe_increasing(values: &[i32]) -> bool {
values
.iter()
Expand All @@ -15,33 +27,21 @@ fn is_safe_decreasing(values: &[i32]) -> bool {
}

fn p1(input: &str) -> String {
input
.trim()
.lines()
.map(|line| {
line.split_whitespace()
.map(|val| val.parse::<i32>().expect("a number"))
.collect::<Vec<_>>()
})
.filter(|line| is_safe_increasing(line) || is_safe_decreasing(line))
parse_input(input)
.into_iter()
.filter(|values| is_safe_increasing(values) || is_safe_decreasing(values))
.count()
.to_string()
}

fn p2(input: &str) -> String {
input
.trim()
.lines()
.map(|line| {
line.split_whitespace()
.map(|val| val.parse::<i32>().expect("a number"))
.collect::<Vec<_>>()
})
.filter(|line| {
is_safe_increasing(line)
|| is_safe_decreasing(line)
|| (0..line.len()).any(|skip_idx| {
let new_values = line
parse_input(input)
.into_iter()
.filter(|values| {
is_safe_increasing(values)
|| is_safe_decreasing(values)
|| (0..values.len()).any(|skip_idx| {
let new_values = values
.iter()
.enumerate()
.filter(|(idx, _)| *idx != skip_idx)
Expand Down

0 comments on commit 2ebabaa

Please sign in to comment.