This commit is contained in:
2018-12-07 08:23:44 +01:00
parent b7a2427912
commit 1fd77f3b83
4 changed files with 1444 additions and 0 deletions

112
2018/03/src/main.rs Normal file
View File

@@ -0,0 +1,112 @@
use std::collections::HashSet;
use std::io::{self, BufRead};
use regex::Regex;
struct Claim {
id: usize,
x: usize,
y: usize,
width: usize,
height: usize,
}
fn main() {
let re = Regex::new(r"#(\d+) @ (\d+),(\d+): (\d+)x(\d+)").expect("failed to build regex");
let claims = io::stdin()
.lock()
.lines()
.filter_map(Result::ok)
.map(|line| {
let caps = re
.captures(&line)
.expect("faild to capture input from line");
Claim {
id: caps
.get(1)
.expect("faild to find id")
.as_str()
.parse::<usize>()
.expect("faild to parse id"),
x: caps
.get(2)
.expect("failed to find x")
.as_str()
.parse::<usize>()
.expect("faild to parse x"),
y: caps
.get(3)
.expect("failed to find y")
.as_str()
.parse::<usize>()
.expect("faild to parse y"),
width: caps
.get(4)
.expect("failed to find width")
.as_str()
.parse::<usize>()
.expect("faild to parse width"),
height: caps
.get(5)
.expect("failed to find height")
.as_str()
.parse::<usize>()
.expect("faild to parse height"),
}
})
.collect::<Vec<_>>();
let width = claims
.iter()
.map(|claim| claim.x + claim.width)
.max()
.expect("failed to find fabric width");
let height = claims
.iter()
.map(|claim| claim.y + claim.height)
.max()
.expect("failed to find fabric height");
let mut fabric = vec![HashSet::new(); width * height].into_boxed_slice();
let mut not_overlapping = HashSet::new();
for claim in &claims {
let mut success = true;
for d_y in claim.y..(claim.y + claim.height) {
let y = d_y * width;
for x in claim.x..(claim.x + claim.width) {
let patch = &mut fabric[y + x];
if !patch.is_empty() {
success = false;
for id in patch.iter() {
not_overlapping.remove(id);
}
}
patch.insert(claim.id);
}
}
if success {
not_overlapping.insert(claim.id);
}
}
let overlaping = fabric.iter().filter(|patch| patch.len() > 1).count();
println!("part.one={}", overlaping);
let not_overlapping = not_overlapping
.into_iter()
.next()
.expect("failed to get first non overlapping claim");
println!("part.two={}", not_overlapping);
}