Skip to content

Instantly share code, notes, and snippets.

@mbrubeck
Forked from SethDusek/main.rs
Last active September 15, 2016 22:48
Show Gist options
  • Save mbrubeck/7aca4e1fe7000ca8b666279ac540eb7b to your computer and use it in GitHub Desktop.
Save mbrubeck/7aca4e1fe7000ca8b666279ac540eb7b to your computer and use it in GitHub Desktop.
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(tile2.clone());
tile2.adjacent.borrow_mut().push(tile.clone());
}
}
tiles.push(tile);
}
}
for tile in &tiles {
println!("{}", tile.adjacent.borrow().len());
}
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);
}
}
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