Add '2018/' from commit '4f35fa85150c24c3c606851be3a8cd5efd6f5500'

git-subtree-dir: 2018
git-subtree-mainline: 5ccd921b23
git-subtree-split: 4f35fa8515
This commit is contained in:
2022-01-09 17:08:32 +00:00
11 changed files with 584 additions and 0 deletions

104
2018/03/part2/src/main.rs Normal file
View File

@@ -0,0 +1,104 @@
#[macro_use] extern crate scan_fmt;
use std::collections::HashSet;
use std::fs::File;
use std::io::BufReader;
use std::io::prelude::*;
#[derive(Hash, Eq, PartialEq, Debug)]
struct Point {
x: usize,
y: usize,
}
#[derive(Hash, Eq, PartialEq, Debug)]
struct Rect {
a: Point,
b: Point,
}
#[derive(Hash, Eq, PartialEq, Debug)]
struct Claim {
id: u32,
area: Rect,
}
impl Claim {
fn new(from: &str) -> Claim {
let (id,x,y,w,h) = scan_fmt!(
from,
"#{} @ {},{}: {}x{}",
u32, usize, usize, usize, usize
);
return Claim{
id: id.unwrap(),
area: Rect {
a: Point {x: x.unwrap(), y: y.unwrap()},
b: Point {x: x.unwrap()+w.unwrap(), y: y.unwrap()+h.unwrap()},
},
}
}
}
fn main() -> std::io::Result<()> {
let f = File::open("../input")?;
let reader = BufReader::new(f);
let mut claims: Vec<Claim> = Vec::new();
let mut max_x = 0;
let mut max_y = 0;
for line in reader.lines() {
let claim = Claim::new(&line.unwrap());
if claim.area.b.x > max_x {
max_x = claim.area.b.x;
}
if claim.area.b.y > max_y {
max_y = claim.area.b.y;
}
claims.push(claim);
}
let mut field: Vec<Vec<Vec<u32>>> = Vec::new();
for _ in 0..max_y {
let mut x_row: Vec<Vec<u32>> = Vec::new();
for _ in 0..max_x {
x_row.push(Vec::new());
}
field.push(x_row);
}
for claim in &claims {
for x in claim.area.a.x..claim.area.b.x {
for y in claim.area.a.y..claim.area.b.y {
field[y][x].push(claim.id);
}
}
}
let mut bad = HashSet::new();
for y in field {
for x in y {
if x.len() > 1 {
for id in x {
bad.insert(id);
}
}
}
}
for claim in &claims {
if !bad.contains(&claim.id) {
println!("{} is good", claim.id)
}
}
Ok(())
}