Use HashSet, space out fn bodies
This commit is contained in:
parent
139e86548c
commit
29d71ea159
@ -1,5 +1,6 @@
|
|||||||
use anyhow::{anyhow, Error, Result};
|
use anyhow::{anyhow, Error, Result};
|
||||||
use common::instrument;
|
use common::instrument;
|
||||||
|
use std::collections::HashSet;
|
||||||
use std::fmt::{self, Display, Formatter};
|
use std::fmt::{self, Display, Formatter};
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
@ -13,13 +14,15 @@ impl FromStr for Cavern {
|
|||||||
type Err = Error;
|
type Err = Error;
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self> {
|
fn from_str(s: &str) -> Result<Self> {
|
||||||
let mut grid: [[u8; 10]; 10] = [[0; 10]; 10];
|
let mut grid = [[0; 10]; 10];
|
||||||
|
|
||||||
for (y, line) in s.trim().lines().enumerate() {
|
for (y, line) in s.trim().lines().enumerate() {
|
||||||
for (x, char) in line.trim().chars().enumerate() {
|
for (x, char) in line.trim().chars().enumerate() {
|
||||||
let energy = char.to_digit(10).ok_or(anyhow!("Invalid energy level"))? as u8;
|
let energy = char.to_digit(10).ok_or(anyhow!("Invalid energy level"))? as u8;
|
||||||
grid[y][x] = energy;
|
grid[y][x] = energy;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Cavern { grid })
|
Ok(Cavern { grid })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -36,13 +39,15 @@ impl Display for Cavern {
|
|||||||
}
|
}
|
||||||
write!(f, "\n")?;
|
write!(f, "\n")?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Cavern {
|
impl Cavern {
|
||||||
fn step(&mut self) -> Vec<(usize, usize)> {
|
fn step(&mut self) -> HashSet<(usize, usize)> {
|
||||||
let mut flashed = vec![];
|
let mut flashed = HashSet::new();
|
||||||
|
|
||||||
for y in 0..10 {
|
for y in 0..10 {
|
||||||
for x in 0..10 {
|
for x in 0..10 {
|
||||||
self.grid[y][x] += 1;
|
self.grid[y][x] += 1;
|
||||||
@ -53,13 +58,13 @@ impl Cavern {
|
|||||||
for x in 0..10 {
|
for x in 0..10 {
|
||||||
let mut to_visit = vec![];
|
let mut to_visit = vec![];
|
||||||
if self.grid[y][x] > 9 && !flashed.contains(&(x, y)) {
|
if self.grid[y][x] > 9 && !flashed.contains(&(x, y)) {
|
||||||
flashed.push((x, y));
|
flashed.insert((x, y));
|
||||||
to_visit.append(&mut self.flash(x, y));
|
to_visit.append(&mut self.flash(x, y));
|
||||||
|
|
||||||
while to_visit.len() > 0 {
|
while to_visit.len() > 0 {
|
||||||
let (x, y) = to_visit.pop().unwrap();
|
let (x, y) = to_visit.pop().unwrap();
|
||||||
if self.grid[y][x] > 9 && !flashed.contains(&(x, y)) {
|
if self.grid[y][x] > 9 && !flashed.contains(&(x, y)) {
|
||||||
flashed.push((x, y));
|
flashed.insert((x, y));
|
||||||
to_visit.append(&mut self.flash(x, y));
|
to_visit.append(&mut self.flash(x, y));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -71,11 +76,13 @@ impl Cavern {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
flashed
|
flashed
|
||||||
}
|
}
|
||||||
|
|
||||||
fn flash(&mut self, x: usize, y: usize) -> Vec<(usize, usize)> {
|
fn flash(&mut self, x: usize, y: usize) -> Vec<(usize, usize)> {
|
||||||
let mut to_visit = vec![];
|
let mut to_visit = vec![];
|
||||||
|
|
||||||
for (dx, dy) in vec![
|
for (dx, dy) in vec![
|
||||||
(-1, -1),
|
(-1, -1),
|
||||||
(-1, 0),
|
(-1, 0),
|
||||||
@ -94,6 +101,7 @@ impl Cavern {
|
|||||||
to_visit.push((x, y));
|
to_visit.push((x, y));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
to_visit
|
to_visit
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user