Skip to content

Instantly share code, notes, and snippets.

@sethdusek
Created June 12, 2016 03:16
Show Gist options
  • Save sethdusek/87e99d5f9481d138d2eb402123c04246 to your computer and use it in GitHub Desktop.
Save sethdusek/87e99d5f9481d138d2eb402123c04246 to your computer and use it in GitHub Desktop.
use std::io::prelude::*;
use std::io::BufReader;
use std::str::FromStr;
use std::fs::File;
#[derive(Copy, Clone, Debug)]
struct Position {
x: usize,
y: usize
}
impl Position {
fn new(x: usize, y: usize) -> Position {
Position { x: x, y: y }
}
fn new_inverse(y: usize, x: usize) -> Position {
Position { x: x, y: y }
}
fn into(&self) -> (usize, usize) {
(self.x, self.y)
}
}
fn square(pos: Position, rocks: &[Position], size: usize) -> usize {
let mut area = 0;
'a: loop {
area+=1;
for rock in rocks {
for y in pos.y..area+1 {
for x in pos.x..area+1 {
if (rock.x as isize - x as isize).abs() == 1 || (rock.y as isize - y as isize).abs() == 1 {
break 'a;
}
}
}
}
}
area
}
fn main() {
let mut file = BufReader::new(File::open("/tmp/field.txt").unwrap());
let mut size_str = String::new();
let mut squares: Vec<(Position, Position)> = Vec::new();
file.read_line(&mut size_str);
let size = usize::from_str(&size_str.trim_right()).unwrap();
let mut rocks = Vec::new();
for (y, line) in (&mut file).lines().take(size).enumerate() {
let line = line.unwrap();
for (idx, x) in line.chars().enumerate() {
if x == 'X' {
rocks.push(Position::new(idx, y));
}
}
}
file.seek(std::io::SeekFrom::Start(0));
let mut buf = String::new();
file.read_to_string(&mut buf);
println!("{}", buf);
let mut area = 0;
for y in 0..size {
'a: for x in 0..size {
for square in &squares {
if (x >= square.0.x && x <= square.1.x) && (y >= square.0.y && y >= square.1.y) {
continue 'a;
}
}
let area2 = square(Position::new(x, y), &rocks, size);
if area2 > area { area = area2; }
squares.push((Position::new(x, y), Position::new(x+area2, y+area2)));
}
}
println!("{} dropships", area);
//println!("{:?}", rocks);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment