-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_06.rs
132 lines (125 loc) · 4.09 KB
/
day_06.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//! https://adventofcode.com/2018/day/6
//! https://adventofcode.com/2018/day/6/input
use std::{fs::read_to_string, time::Instant};
fn parse(input: &str) -> Vec<(usize, usize)> {
input
.lines()
.map(|line| {
let parts: Vec<_> = line.split(", ").collect();
(parts[1].parse().unwrap(), parts[0].parse().unwrap())
})
.collect()
}
pub mod part1 {
use super::parse;
pub fn solve(input: &str) -> usize {
let points = parse(input);
let (max_i, max_j) = points
.iter()
.fold((usize::MIN, usize::MIN), |(max_i, max_j), (x, y)| {
(max_i.max(*x), max_j.max(*y))
});
let mut distances = vec![vec![vec![usize::MAX; points.len()]; max_j + 1]; max_i + 1];
for (i, row) in distances.iter_mut().enumerate() {
for (j, point) in row.iter_mut().enumerate() {
for (p, (x, y)) in points.iter().enumerate() {
point[p] = (i as isize - *x as isize).unsigned_abs()
+ (j as isize - *y as isize).unsigned_abs();
}
}
}
let mut nearest = vec![vec![None; max_j + 1]; max_i + 1];
for (i, row) in distances.iter_mut().enumerate() {
for (j, point) in row.iter_mut().enumerate() {
let (p, min_distance) = point
.iter()
.enumerate()
.min_by_key(|(_, distance)| **distance)
.unwrap();
if point
.iter()
.filter(|distance| *distance == min_distance)
.count()
== 1
{
nearest[i][j] = Some(p);
}
}
}
let mut valid = vec![true; points.len()];
for j in 0..nearest[0].len() {
if let Some(p) = nearest[0][j] {
valid[p] = false;
}
if let Some(p) = nearest[nearest.len() - 1][j] {
valid[p] = false;
}
}
for i in 0..nearest.len() {
if let Some(p) = nearest[i][0] {
valid[p] = false;
}
if let Some(p) = nearest[i][nearest[0].len() - 1] {
valid[p] = false;
}
}
let mut largest = vec![0_usize; points.len()];
for row in nearest {
for p in row.into_iter().flatten() {
largest[p] += 1;
}
}
*largest
.iter()
.enumerate()
.filter(|(i, _)| valid[*i])
.max_by_key(|(_, area)| **area)
.unwrap()
.1
}
}
pub mod part2 {
use super::parse;
pub fn solve(input: &str, max_distance: usize) -> usize {
let points = parse(input);
let (max_i, max_j) = points
.iter()
.fold((usize::MIN, usize::MIN), |(max_i, max_j), (x, y)| {
(max_i.max(*x), max_j.max(*y))
});
let mut distances = vec![vec![0; max_j + 1]; max_i + 1];
for (i, row) in distances.iter_mut().enumerate() {
for (j, point) in row.iter_mut().enumerate() {
for (x, y) in points.iter() {
*point += (i as isize - *x as isize).unsigned_abs()
+ (j as isize - *y as isize).unsigned_abs();
}
}
}
distances
.iter()
.flat_map(|row| row.iter())
.filter(|distance| **distance < max_distance)
.count()
}
}
pub fn main(test: bool) {
let test_input = "1, 1
1, 6
8, 3
3, 4
5, 5
8, 9"
.to_owned();
let (puzzle_input, max_distance) = if test {
(test_input, 32)
} else {
(read_to_string("../inputs/2018/day_06_input.txt").unwrap(), 10000)
};
let start = Instant::now();
println!("{}", part1::solve(&puzzle_input));
println!("Run in {:?}", start.elapsed());
let start = Instant::now();
println!("{}", part2::solve(&puzzle_input, max_distance));
println!("Run in {:?}", start.elapsed());
}