Skip to content

Commit

Permalink
day 02
Browse files Browse the repository at this point in the history
  • Loading branch information
Mishco committed Dec 2, 2024
1 parent 27fb4ab commit 75bef6d
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 4 deletions.
6 changes: 6 additions & 0 deletions data/examples/02.txt
Original file line number Diff line number Diff line change
@@ -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
118 changes: 114 additions & 4 deletions src/bin/02.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,121 @@
advent_of_code::solution!(2);

pub fn part_one(input: &str) -> Option<u32> {
None
//increasing , decs
let mut safe_count = 0;
let mut unsafe_count = 0;
for line in input.lines() {
println!("{}", line);

let numbers: Vec<i32> = line.split_whitespace().map(|item| {
item.parse::<i32>().unwrap()
}).collect();

if checking_level(&numbers) {
safe_count += 1;
} else {
unsafe_count += 1;
}
}

println!("{}",safe_count);
// if let Some(value) = checking_level(numbers) {
// return value;
// }

// if numbers.iter().max().unwrap() - numbers.iter().min().unwrap() > 3 {
// // unsafe
// unsafe_count+=1;
// } else {
// safe_count+=1;
// }

// if let (Some(max), Some(min)) = (numbers.iter().max(), numbers.iter().min()) {
// if max - min > 3 {
// unsafe_count += 1;
// } else {
// safe_count += 1;
// }
// }


Some(safe_count as u32)

}

fn checking_level(numbers: &Vec<i32>) -> bool {
let mut is_increasing = true;
let mut is_decreasing = true;

for i in 0..numbers.len() - 1 {
// let diff = (&numbers - &numbers).abs();
let curr = numbers[i];
let next = numbers[i + 1];

let diff = (next - curr).abs();

if diff < 1 || diff > 3 {
//unsafe
// unsafe_count += 1;
return false
}

if curr < next {
//decreasing
// safe_count += 1;
is_decreasing = false;
// return false;
} else if curr > next {
//increas
// safe_count += 1;
is_increasing= false;
// return Some(false)
} else {
// nochagning
return false;
// return Some(false);
}
}
is_increasing || is_decreasing
}

pub fn part_two(input: &str) -> Option<u32> {
None
let mut safe_count = 0;
let mut unsafe_count = 0;
for line in input.lines() {
println!("{}", line);

let numbers: Vec<i32> = line.split_whitespace().map(|item| {
item.parse::<i32>().unwrap()
}).collect();

if checking_level(&numbers) {
safe_count += 1;
} else {
// try to remove item and check again
for i in 0..numbers.len() {
let mut cleaned_level = numbers.to_vec();
cleaned_level.remove(i);


if checking_level(&cleaned_level) {
//
safe_count += 1;
break;
}
}
// not safe for sure
unsafe_count += 1;
}
// if checking_level(&numbers) {
// safe_count += 1;
// } else {
// unsafe_count += 1;
// }
}

println!("{}",safe_count);
Some(safe_count as u32)
}

#[cfg(test)]
Expand All @@ -15,12 +125,12 @@ mod tests {
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, None);
assert_eq!(result, Some(2));
}

#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, None);
assert_eq!(result, Some(4));
}
}

0 comments on commit 75bef6d

Please sign in to comment.