Created
December 4, 2018 03:07
-
-
Save benzap/42d8431292909905900dcee4d9f6c90b to your computer and use it in GitHub Desktop.
Broken Implementation for Problem 3
This file contains 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
// Problem 3 | |
#![allow(unused)] | |
extern crate regex; | |
use std::default::Default; | |
use std::collections::HashMap; | |
use std::collections::HashSet; | |
use std::io::BufReader; | |
use std::io::BufRead; | |
use std::fs::File; | |
use std::io::SeekFrom; | |
use std::io::Seek; | |
use regex::Regex; | |
use regex::Captures; | |
#[derive(Debug)] | |
struct FabricPiece { | |
claimed: Vec<u32>, | |
} | |
struct Point { | |
x: u32, | |
y: u32, | |
} | |
struct Size { | |
w: u32, | |
h: u32, | |
} | |
struct ParseResult { | |
id: u32, | |
offset: Point, | |
size: Size, | |
} | |
type Index = (u32, u32); | |
type FabricMap = HashMap<Index, FabricPiece>; | |
fn to_uint(capture: &Captures, group_name: String) -> u32 { | |
capture.name("id").unwrap().as_str().parse::<u32>().unwrap() | |
} | |
fn parse_line(s: &String) -> ParseResult { | |
let re = Regex::new(r"#(?P<id>\d+) @ (?P<x>\d+),(?P<y>\d+): (?P<w>\d+)x(?P<h>\d+)").unwrap(); | |
let caps = re.captures(s).unwrap(); | |
let id = to_uint(&caps, String::from("id")); | |
let offset_x = to_uint(&caps, String::from("x")); | |
let offset_y = to_uint(&caps, String::from("y")); | |
let size_w = to_uint(&caps, String::from("w")); | |
let size_h = to_uint(&caps, String::from("h")); | |
ParseResult{ | |
id: id, | |
offset: Point{x: offset_x, y: offset_y}, | |
size: Size{w: size_w, h: size_h}, | |
} | |
} | |
fn update_map_point(m: &mut FabricMap, id: u32, index: Index) { | |
if let Some(fabric) = m.get_mut(&index) { | |
fabric.claimed.push(id); | |
} | |
else { | |
let mut c = Vec::new(); | |
c.push(id); | |
let f = FabricPiece{claimed: c}; | |
m.insert(index, f); | |
} | |
} | |
fn update_map(m: &mut FabricMap, r: &ParseResult) { | |
println!("Processing Id: {}...", r.id); | |
for i in 0..r.size.w-1 { | |
for j in 0..r.size.h-1 { | |
let index = ((r.offset.x + i), (r.offset.y + j)); | |
update_map_point(m, r.id, index); | |
} | |
} | |
} | |
fn process_line(mut m: &mut FabricMap, s: &String) { | |
let r = parse_line(s); | |
update_map(&mut m, &r); | |
} | |
fn get_overlaps(m: &FabricMap) -> u32 { | |
let mut total_overlap = 0; | |
for f in m.values() { | |
if f.claimed.len() > 1 { | |
total_overlap += 1; | |
} | |
} | |
total_overlap | |
} | |
fn main() { | |
let file_handle = File::open("resources/p3.txt").unwrap(); | |
let buffer_reader = BufReader::new(&file_handle); | |
let mut map: FabricMap = HashMap::new(); | |
for line in buffer_reader.lines() { | |
let l = line.unwrap(); | |
process_line(&mut map, &l); | |
} | |
let index = (26, 26); | |
if let Some(fabric) = map.get_mut(&index) { | |
println!("Did fabric update? {} - {}, {}", fabric.claimed.len(), index.0, index.1); | |
} | |
println!("Total Number of Overlaps: {}", get_overlaps(&map)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment