Skip to content

Commit 500128a

Browse files
committed
Improved Day24 to use a single byte
1 parent a0cb289 commit 500128a

File tree

1 file changed

+11
-18
lines changed

1 file changed

+11
-18
lines changed

2022/src/bin/day24.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use advent_lib::{
55
direction::Direction::{self, *},
66
geometry::point2,
77
grid::{Grid, Location},
8-
iter_utils::CountIf,
98
math::greatest_common_divisor,
109
search::{a_star_search, SearchGraph, SearchGraphWithGoal},
1110
*,
@@ -25,14 +24,14 @@ enum InputBlock {
2524
}
2625

2726
#[derive(Default, Copy, Clone, PartialEq)]
28-
struct Storm([bool; 4]);
27+
struct Storm(u8);
2928

3029
impl Storm {
31-
const EMPTY: Storm = Storm([false; 4]);
30+
const EMPTY: Storm = Storm(0);
3231

33-
fn set_blow(&mut self, dir: Direction) { self.0[usize::from(dir)] = true }
32+
fn set_blow(&mut self, dir: Direction) { self.0 |= 1 << usize::from(dir) }
3433

35-
fn get_blow(&self, dir: Direction) -> bool { self.0[usize::from(dir)] }
34+
fn get_blow(&self, dir: Direction) -> bool { self.0 & (1 << usize::from(dir)) != 0 }
3635
}
3736

3837
impl From<Direction> for Storm {
@@ -54,19 +53,13 @@ impl From<InputBlock> for Storm {
5453

5554
impl From<Storm> for char {
5655
fn from(value: Storm) -> Self {
57-
let count = value.0.count_if(|b| *b) as u32;
58-
if count == 0 {
59-
'.'
60-
} else if count == 1 {
61-
match value.0 {
62-
[true, false, false, false] => '>',
63-
[false, true, false, false] => 'v',
64-
[false, false, true, false] => '<',
65-
[false, false, false, true] => '^',
66-
_ => 'X',
67-
}
68-
} else {
69-
char::from_digit(count, 10).unwrap()
56+
match value.0 {
57+
0 => '.',
58+
1 => '>',
59+
2 => 'v',
60+
4 => '<',
61+
8 => '^',
62+
_ => char::from_digit(value.0.count_ones(), 10).unwrap(),
7063
}
7164
}
7265
}

0 commit comments

Comments
 (0)