Skip to content

Commit

Permalink
chore: calculate boundaries once
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaxwood committed Aug 11, 2023
1 parent 061d962 commit b684114
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions src/day24/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ use nom::{

struct State {
map: HashMap<Coord, Vec<Pixel>>,
height: usize,
width: usize,
start: Coord,
end: Coord,
}

type Coord = (usize, usize);
Expand Down Expand Up @@ -59,33 +63,38 @@ fn parse(input: &str) -> IResult<&str, HashMap<Coord, Vec<Pixel>>> {
}

impl State {
fn find_start_end(&self) -> (Coord, Coord) {
let start = self.map

fn set_start_end(&mut self) {
let start = self
.map
.iter()
.filter(|(k, _)| k.1 == 0)
.filter(|(_, v)| **v == vec![Pixel::Open])
.map(|(k, _)| k)
.next()
.unwrap();
let row_max = self.map.keys().max_by_key(|(_, y)| y).unwrap().1;
let end = self.map
let end = self
.map
.iter()
.filter(|(k, _)| k.1 == row_max)
.filter(|(_, v)| **v == vec![Pixel::Open])
.map(|(k, _)| k)
.next()
.unwrap();
(*start, *end)

self.start = *start;
self.end = *end;
}

fn find_width_height(&self) -> (usize, usize) {
fn set_width_height(&mut self) {
let col_max = self.map.keys().max_by_key(|(x, _)| x).unwrap().0 - 1;
let row_max = self.map.keys().max_by_key(|(_, y)| y).unwrap().1 - 1;
(col_max, row_max)
self.width = col_max;
self.height = row_max;
}

fn tick(&mut self) {
let (width, height) = self.find_width_height();
for ((x, y), pixels) in self.map.iter_mut() {
for pixel in pixels {
match pixel {
Expand All @@ -105,9 +114,19 @@ impl State {
fn day24a(path: &str) -> usize {
let input = fs::read_to_string(path).unwrap();
let (_, map) = parse(&input).unwrap();
let mut state = State { map: map.clone() };
let (start, end) = state.find_start_end();
let mut state = State {
map: map.clone(),
width: 0,
height: 0,
start: (0, 0),
end: (0, 0),
};

state.set_width_height();
state.set_start_end();

state.tick();

0
}

Expand All @@ -122,3 +141,4 @@ mod tests {
assert_eq!(actual, 18);
}
}

0 comments on commit b684114

Please sign in to comment.