From 01fe0c7d69cfc15c44bf8da9e7bc12c4b4ced264 Mon Sep 17 00:00:00 2001 From: omagdy7 Date: Wed, 12 Apr 2023 04:58:02 +0200 Subject: Day11 done in rust --- 2022/Rust/src/day10.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to '2022/Rust/src/day10.rs') diff --git a/2022/Rust/src/day10.rs b/2022/Rust/src/day10.rs index 992c503..92a27d4 100644 --- a/2022/Rust/src/day10.rs +++ b/2022/Rust/src/day10.rs @@ -27,13 +27,12 @@ impl Crt { } } - fn get_overlap_with_sprite(&self, sprite: &Sprite, cycle: i32) -> Option { + fn is_overlapping_with_sprite(&self, sprite: &Sprite, cycle: i32) -> Option { let x = cycle % 40; - if x == sprite.location || x == sprite.location - 1 || x == sprite.location + 1 { - Some(x) - } else { - None + if ((x - 1)..=(x + 1)).contains(&sprite.location) { + return Some(x); } + None } } @@ -129,7 +128,7 @@ fn solve_part_two(data: &str) { y_val += 1; } } - match crt.get_overlap_with_sprite(&sprite, cur_cycle) { + match crt.is_overlapping_with_sprite(&sprite, cur_cycle) { Some(idx) => crt.pixels[y_val as usize] .replace_range((idx as usize)..=idx as usize, "#"), None => {} @@ -144,7 +143,7 @@ fn solve_part_two(data: &str) { y_val += 1; } } - match crt.get_overlap_with_sprite(&sprite, cur_cycle) { + match crt.is_overlapping_with_sprite(&sprite, cur_cycle) { Some(idx) => { crt.pixels[y_val as usize].replace_range((idx as usize)..=idx as usize, "#") } -- cgit v1.2.3