From 4a8c5580ff22d2e2cb5d9f52514ef4cff856c5f8 Mon Sep 17 00:00:00 2001 From: Bas Dado Date: Fri, 11 Dec 2020 22:31:05 +0100 Subject: [PATCH] [TASK] Added some padding to the grid to save some time on out-of-bounds checks, but it's still pretty slow --- src/day11.rs | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/src/day11.rs b/src/day11.rs index 473977e..13d7265 100644 --- a/src/day11.rs +++ b/src/day11.rs @@ -6,7 +6,7 @@ use crate::day11::GridState::EmptySeat; pub fn solve() { let lines = util::read_file("input/day11.txt"); - let seats = Seats::parse(&lines); + let seats = Seats::parse(&lines, 1); let part1 = solve_part1(&seats); @@ -25,8 +25,8 @@ fn solve_part1(seats: &Seats) -> usize { loop { let mut changes = false; - for y in 0..seats.height { - for x in 0..seats.width { + for y in 1..seats.height-1 { + for x in 1..seats.width-1 { let seat = last_seats.get(&x, &y); if seat != &Floor { let occupied = last_seats.count_occupied_neighbors(&x, &y); @@ -66,8 +66,8 @@ fn solve_part2(seats: &Seats, visible_chairs: &Vec>>) changes = true; next_seats.set(&x, &y, FilledSeat); } else if seat == &FilledSeat && occupied >= 5 { - next_seats.set(&x, &y, EmptySeat); changes = true; + next_seats.set(&x, &y, EmptySeat); } } } @@ -94,16 +94,16 @@ impl Seats { const DIRECTIONS: [(i32, i32); 8] = [(-1, 1), (-1, 0), (-1, -1), (0, -1), (0, 1), (1, 1), (1, 0), (1, -1)]; - fn parse(lines: &Vec) -> Seats { + fn parse(lines: &Vec, padding: usize) -> Seats { - let height = lines.len(); - let width = lines[0].len(); + let height = lines.len() + padding * 2; + let width = lines[0].len() + padding * 2; let mut grid = vec!(GridState::Floor; width * height); for y in 0..lines.len() { let line = &lines[y]; let mut line_chars = line.chars(); for x in 0..lines[0].len() { - grid[x + y * width] = GridState::parse(&line_chars.next().unwrap()) + grid[(x + padding) + (y + padding) * width] = GridState::parse(&line_chars.next().unwrap()) } } @@ -170,20 +170,15 @@ impl Seats { fn count_occupied_neighbors(&self, x: &usize, y: &usize) -> u8 { - let x_upper = x == &(self.width - &ONE); - let x_lower = x == &0; - let y_upper = y == &(self.height - &ONE); - let y_lower = y == &0; - return - if !x_lower && !y_lower && self.is_occupied(&(x - &ONE), &(y - &ONE)) { 1 } else { 0 } + - if !x_lower && self.is_occupied(&(x - &ONE), &y) { 1 } else { 0 } + - if !x_lower && !y_upper && self.is_occupied(&(x - &ONE), &(y + ONE)) { 1 } else { 0 } + - if !y_lower && self.is_occupied(&x, &(y - ONE)) { 1 } else { 0 } + - if !y_upper && self.is_occupied(&x, &(y + ONE)) { 1 } else { 0 } + - if !x_upper && !y_lower && self.is_occupied(&(x + ONE), &(y - ONE)) { 1 } else { 0 } + - if !x_upper && self.is_occupied(&(x + ONE), &y) { 1 } else { 0 } + - if !x_upper && !y_upper && self.is_occupied(&(x + ONE), &(y + ONE)) { 1 } else { 0 } + if self.is_occupied(&(x - &ONE), &(y - &ONE)) { 1 } else { 0 } + + if self.is_occupied(&(x - &ONE), &y) { 1 } else { 0 } + + if self.is_occupied(&(x - &ONE), &(y + ONE)) { 1 } else { 0 } + + if self.is_occupied(&x, &(y - ONE)) { 1 } else { 0 } + + if self.is_occupied(&x, &(y + ONE)) { 1 } else { 0 } + + if self.is_occupied(&(x + ONE), &(y - ONE)) { 1 } else { 0 } + + if self.is_occupied(&(x + ONE), &y) { 1 } else { 0 } + + if self.is_occupied(&(x + ONE), &(y + ONE)) { 1 } else { 0 } }