-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_03.rs
106 lines (95 loc) · 2.95 KB
/
day_03.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//! https://adventofcode.com/2018/day/3
//! https://adventofcode.com/2018/day/3/input
use std::{fs::read_to_string, time::Instant};
use regex::Regex;
#[derive(Copy, Clone, Debug, Default)]
struct Rectangle {
id: usize,
left: usize,
top: usize,
width: usize,
height: usize,
}
fn parse(input: &str) -> Vec<Rectangle> {
let pattern = Regex::new(r"#(\d+) @ (\d+),(\d+): (\d+)x(\d+)").unwrap();
input
.lines()
.map(|line| {
let captures = pattern.captures(line).unwrap();
Rectangle {
id: captures[1].parse().unwrap(),
left: captures[2].parse().unwrap(),
top: captures[3].parse().unwrap(),
width: captures[4].parse().unwrap(),
height: captures[5].parse().unwrap(),
}
})
.collect()
}
pub mod part1 {
use std::collections::HashMap;
use super::parse;
pub fn solve(input: &str) -> usize {
let rectangles = parse(input);
let mut fabric = HashMap::new();
for rectangle in rectangles {
for i in rectangle.top..rectangle.top + rectangle.height {
for j in rectangle.left..rectangle.left + rectangle.width {
*fabric.entry((i, j)).or_insert(0) += 1;
}
}
}
fabric.values().filter(|&&v| v >= 2).count()
}
}
pub mod part2 {
use std::collections::HashMap;
use super::parse;
pub fn solve(input: &str) -> usize {
let rectangles = parse(input);
let mut fabric = HashMap::new();
for rectangle in &rectangles {
for i in rectangle.top..rectangle.top + rectangle.height {
for j in rectangle.left..rectangle.left + rectangle.width {
*fabric.entry((i, j)).or_insert(0) += 1;
}
}
}
for rectangle in rectangles {
let mut overlaps = false;
for i in rectangle.top..rectangle.top + rectangle.height {
for j in rectangle.left..rectangle.left + rectangle.width {
if let Some(n) = fabric.get(&(i, j)) {
if *n > 1 {
overlaps = true;
}
}
}
if overlaps {
break;
}
}
if !overlaps {
return rectangle.id;
}
}
0
}
}
pub fn main(test: bool) {
let test_input = "#1 @ 1,3: 4x4
#2 @ 3,1: 4x4
#3 @ 5,5: 2x2"
.to_owned();
let puzzle_input = if test {
test_input
} else {
read_to_string("../inputs/2018/day_03_input.txt").unwrap()
};
let start = Instant::now();
println!("{}", part1::solve(&puzzle_input));
println!("Run in {:?}", start.elapsed());
let start = Instant::now();
println!("{}", part2::solve(&puzzle_input));
println!("Run in {:?}", start.elapsed());
}