Watch
1
0
Fork
You've already forked advent-of-code
0
This commit is contained in:
Jeremy Kaplan 2018-12-03 20:52:57 -08:00
commit db4c3d6542
2 changed files with 100 additions and 0 deletions

98
2018/src/day3.rs Normal file
View file

@ -0,0 +1,98 @@
use std::collections::HashMap;
const INPUT: &str = include_str!("input/day3.txt");
struct Rectangle {
id: String,
x: u32,
y: u32,
w: u32,
h: u32,
}
fn parse_input(input: &str) -> Vec<Rectangle> {
let mut rects = Vec::new();
for line in str::lines(input) {
// #id @ x,y: wxh
let mut tokens = Vec::new();
let mut tok = "".to_string();
for c in line.chars() {
match c {
' ' | ',' | 'x' | '@' => {
if !tok.is_empty() {
tokens.push(tok);
}
tok = "".to_string();
}
'#' | ':' => (),
_ => tok.push(c),
}
}
if !tok.is_empty() {
tokens.push(tok);
}
let id = tokens[0].to_string();
let x = tokens[1].parse::<u32>().unwrap();
let y = tokens[2].parse::<u32>().unwrap();
let w = tokens[3].parse::<u32>().unwrap();
let h = tokens[4].parse::<u32>().unwrap();
rects.push(Rectangle {
id: id,
x: x,
y: y,
w: w,
h: h,
})
}
rects
}
pub fn part1() -> u32 {
let mut squares = HashMap::new();
for r in parse_input(INPUT) {
for x in r.x..(r.x + r.w) {
for y in r.y..(r.y + r.h) {
let mut count = squares.entry((x, y)).or_insert(0);
*count += 1;
}
}
}
let mut count = 0;
for (_, layers) in squares {
if layers > 1 {
count += 1;
}
}
count
}
fn uncovered(grid: &HashMap<(u32, u32), u32>, r: &Rectangle) -> bool {
for x in r.x..(r.x + r.w) {
for y in r.y..(r.y + r.h) {
match grid.get(&(x, y)) {
Some(1) => {}
_ => return false,
}
}
}
true
}
pub fn part2() -> String {
let mut squares = HashMap::new();
let rects = parse_input(INPUT);
for r in &rects {
for x in r.x..(r.x + r.w) {
for y in r.y..(r.y + r.h) {
let mut count = squares.entry((x, y)).or_insert(0);
*count += 1;
}
}
}
for r in &rects {
if uncovered(&squares, r) {
return r.id.to_owned();
}
}
panic!("No solution!")
}

View file

@ -7,4 +7,6 @@ fn main() {
println!("Day 1 part 2: {}", day1::part2());
println!("Day 2 part 1: {}", day2::part1());
println!("Day 2 part 2: {}", day2::part2());
println!("Day 3 part 1: {}", day3::part1());
println!("Day 3 part 2: {}", day3::part2());
}