Created
September 15, 2016 22:24
-
-
Save sethdusek/80919e6d390f58693515fb57146577a9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io::stdin; | |
use std::io::Read; | |
use std::rc::Rc; | |
use std::cell::RefCell; | |
use std::cmp::max; | |
#[derive(Debug, Copy, Clone)] | |
struct Position(u16, u16); | |
impl Position { | |
fn adjacent(&self, other: &Position) -> bool { | |
max((self.0 as i16 - other.0 as i16).abs(), (self.1 as i16 - other.1 as i16).abs()) <= 1 | |
} | |
} | |
#[derive(Debug)] | |
struct Tile { | |
pos: Position, | |
tile: Type, | |
adjacent: RefCell<Vec<Rc<Tile>>> | |
} | |
#[derive(Debug, Copy, Clone)] | |
enum Type { | |
Blank, | |
Adjacent(u8), | |
Unknown | |
} | |
fn input() -> Vec<Rc<Tile>> { | |
let mut stdin = stdin(); | |
let mut buf = String::new(); | |
let _ = stdin.read_to_string(&mut buf); | |
let mut tiles = Vec::<Rc<Tile>>::new(); | |
for (y, row) in buf.lines().enumerate() { | |
for (x, column) in row.chars().enumerate() { | |
let tile_type = match column { | |
'?' => Type::Unknown, | |
' ' => Type::Blank, | |
_ => Type::Adjacent(column.to_digit(10).unwrap() as u8) | |
}; | |
let tile = Rc::new(Tile { | |
pos: Position(x as u16, y as u16), | |
tile: tile_type, | |
adjacent: RefCell::new(Vec::with_capacity(8)) | |
}); | |
for tile2 in &mut tiles { | |
if tile.pos.adjacent(&tile2.pos) { | |
tile.adjacent.borrow_mut().push(tile.clone()); | |
} | |
} | |
println!("{}", tile.adjacent.borrow().len()); | |
tiles.push(tile); | |
} | |
} | |
tiles | |
} | |
fn main() { | |
let tiles = input(); | |
for tile in tiles.into_iter().filter(|tile| if let Type::Adjacent(num) = tile.tile { num==tile.adjacent.borrow().len() as u8 } else { false }) { | |
println!("{:?}", tile.pos); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1???? | |
1???? | |
111?? | |
1?? | |
1211 1?? | |
???21 1?? | |
????211?? | |
????????? | |
????????? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment