[TASK] Added some padding to the grid to save some time on out-of-bounds checks, but it's still pretty slow
This commit is contained in:
37
src/day11.rs
37
src/day11.rs
@@ -6,7 +6,7 @@ use crate::day11::GridState::EmptySeat;
|
|||||||
pub fn solve() {
|
pub fn solve() {
|
||||||
let lines = util::read_file("input/day11.txt");
|
let lines = util::read_file("input/day11.txt");
|
||||||
|
|
||||||
let seats = Seats::parse(&lines);
|
let seats = Seats::parse(&lines, 1);
|
||||||
|
|
||||||
let part1 = solve_part1(&seats);
|
let part1 = solve_part1(&seats);
|
||||||
|
|
||||||
@@ -25,8 +25,8 @@ fn solve_part1(seats: &Seats) -> usize {
|
|||||||
|
|
||||||
loop {
|
loop {
|
||||||
let mut changes = false;
|
let mut changes = false;
|
||||||
for y in 0..seats.height {
|
for y in 1..seats.height-1 {
|
||||||
for x in 0..seats.width {
|
for x in 1..seats.width-1 {
|
||||||
let seat = last_seats.get(&x, &y);
|
let seat = last_seats.get(&x, &y);
|
||||||
if seat != &Floor {
|
if seat != &Floor {
|
||||||
let occupied = last_seats.count_occupied_neighbors(&x, &y);
|
let occupied = last_seats.count_occupied_neighbors(&x, &y);
|
||||||
@@ -66,8 +66,8 @@ fn solve_part2(seats: &Seats, visible_chairs: &Vec<Option<Vec<(usize, usize)>>>)
|
|||||||
changes = true;
|
changes = true;
|
||||||
next_seats.set(&x, &y, FilledSeat);
|
next_seats.set(&x, &y, FilledSeat);
|
||||||
} else if seat == &FilledSeat && occupied >= 5 {
|
} else if seat == &FilledSeat && occupied >= 5 {
|
||||||
next_seats.set(&x, &y, EmptySeat);
|
|
||||||
changes = true;
|
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)];
|
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<String>) -> Seats {
|
fn parse(lines: &Vec<String>, padding: usize) -> Seats {
|
||||||
|
|
||||||
let height = lines.len();
|
let height = lines.len() + padding * 2;
|
||||||
let width = lines[0].len();
|
let width = lines[0].len() + padding * 2;
|
||||||
let mut grid = vec!(GridState::Floor; width * height);
|
let mut grid = vec!(GridState::Floor; width * height);
|
||||||
for y in 0..lines.len() {
|
for y in 0..lines.len() {
|
||||||
let line = &lines[y];
|
let line = &lines[y];
|
||||||
let mut line_chars = line.chars();
|
let mut line_chars = line.chars();
|
||||||
for x in 0..lines[0].len() {
|
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 {
|
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
|
return
|
||||||
if !x_lower && !y_lower && self.is_occupied(&(x - &ONE), &(y - &ONE)) { 1 } else { 0 } +
|
if self.is_occupied(&(x - &ONE), &(y - &ONE)) { 1 } else { 0 } +
|
||||||
if !x_lower && self.is_occupied(&(x - &ONE), &y) { 1 } else { 0 } +
|
if 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 self.is_occupied(&(x - &ONE), &(y + ONE)) { 1 } else { 0 } +
|
||||||
if !y_lower && self.is_occupied(&x, &(y - ONE)) { 1 } else { 0 } +
|
if self.is_occupied(&x, &(y - ONE)) { 1 } else { 0 } +
|
||||||
if !y_upper && self.is_occupied(&x, &(y + ONE)) { 1 } else { 0 } +
|
if 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 self.is_occupied(&(x + ONE), &(y - ONE)) { 1 } else { 0 } +
|
||||||
if !x_upper && self.is_occupied(&(x + ONE), &y) { 1 } else { 0 } +
|
if 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 }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user