2018/06
This commit is contained in:
4
2018/06/Cargo.lock
generated
Normal file
4
2018/06/Cargo.lock
generated
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
[[package]]
|
||||||
|
name = "day-06"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
6
2018/06/example-01.txt
Normal file
6
2018/06/example-01.txt
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
1, 1
|
||||||
|
1, 6
|
||||||
|
8, 3
|
||||||
|
3, 4
|
||||||
|
5, 5
|
||||||
|
8, 9
|
||||||
50
2018/06/input.txt
Normal file
50
2018/06/input.txt
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
194, 200
|
||||||
|
299, 244
|
||||||
|
269, 329
|
||||||
|
292, 55
|
||||||
|
211, 63
|
||||||
|
123, 311
|
||||||
|
212, 90
|
||||||
|
292, 169
|
||||||
|
359, 177
|
||||||
|
354, 95
|
||||||
|
101, 47
|
||||||
|
95, 79
|
||||||
|
95, 287
|
||||||
|
294, 126
|
||||||
|
81, 267
|
||||||
|
330, 78
|
||||||
|
202, 165
|
||||||
|
225, 178
|
||||||
|
266, 272
|
||||||
|
351, 326
|
||||||
|
180, 62
|
||||||
|
102, 178
|
||||||
|
151, 101
|
||||||
|
343, 145
|
||||||
|
205, 312
|
||||||
|
74, 193
|
||||||
|
221, 56
|
||||||
|
89, 89
|
||||||
|
242, 172
|
||||||
|
59, 138
|
||||||
|
83, 179
|
||||||
|
223, 88
|
||||||
|
297, 234
|
||||||
|
147, 351
|
||||||
|
226, 320
|
||||||
|
358, 338
|
||||||
|
321, 172
|
||||||
|
54, 122
|
||||||
|
263, 165
|
||||||
|
126, 341
|
||||||
|
64, 132
|
||||||
|
264, 306
|
||||||
|
72, 202
|
||||||
|
98, 49
|
||||||
|
238, 67
|
||||||
|
310, 303
|
||||||
|
277, 281
|
||||||
|
222, 318
|
||||||
|
357, 169
|
||||||
|
123, 225
|
||||||
@@ -1,3 +1,115 @@
|
|||||||
fn main() {
|
use std::collections::HashMap;
|
||||||
println!("Hello, world!");
|
use std::io::{self, BufRead};
|
||||||
|
|
||||||
|
fn distance(a: (i32, i32), b: (i32, i32)) -> i32 {
|
||||||
|
(a.0 - b.0).abs() + (a.1 - b.1).abs()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
enum State {
|
||||||
|
Empty,
|
||||||
|
Owner(usize, i32),
|
||||||
|
Equal(i32),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
enum Area {
|
||||||
|
Infinite,
|
||||||
|
Finite(usize),
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let coordinates = io::stdin()
|
||||||
|
.lock()
|
||||||
|
.lines()
|
||||||
|
.filter_map(Result::ok)
|
||||||
|
.map(|line| {
|
||||||
|
let coords = line
|
||||||
|
.split(", ")
|
||||||
|
.map(|coord| coord.parse::<i32>().unwrap())
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
(coords[0], coords[1])
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
let width = coordinates
|
||||||
|
.iter()
|
||||||
|
.map(|(x, _)| *x as usize)
|
||||||
|
.max().expect("failed to find max x") + 1;
|
||||||
|
|
||||||
|
let height = coordinates
|
||||||
|
.iter()
|
||||||
|
.map(|(_, y)| *y as usize)
|
||||||
|
.max()
|
||||||
|
.expect("failed to find max y") + 1;
|
||||||
|
|
||||||
|
let mut grid_view = vec![State::Empty; width * height].into_boxed_slice();
|
||||||
|
|
||||||
|
for x in 0..width {
|
||||||
|
for y in 0..height {
|
||||||
|
for (i, c) in coordinates.iter().enumerate() {
|
||||||
|
let distance = distance(*c, (x as i32, y as i32));
|
||||||
|
let state = &mut grid_view[x + (y * width)];
|
||||||
|
|
||||||
|
*state = match state {
|
||||||
|
State::Empty => State::Owner(i, distance),
|
||||||
|
State::Owner(_, d) if *d > distance => State::Owner(i, distance),
|
||||||
|
State::Owner(o, d) if *d == distance && *o != i => State::Equal(distance),
|
||||||
|
State::Equal(d) if *d > distance => State::Owner(i, distance),
|
||||||
|
_ => *state,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut areas = HashMap::new();
|
||||||
|
|
||||||
|
for x in 0..width {
|
||||||
|
for y in 0..height {
|
||||||
|
if let State::Owner(o, _) = grid_view[x + (y * width)] {
|
||||||
|
let a = areas.entry(o).or_insert(Area::Finite(0));
|
||||||
|
|
||||||
|
if x == 0 || x == width - 1 || y == 0 || y == height - 1 {
|
||||||
|
*a = Area::Infinite;
|
||||||
|
} else {
|
||||||
|
match a {
|
||||||
|
Area::Infinite => (),
|
||||||
|
Area::Finite(area) => *area += 1,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let (_, area) = areas
|
||||||
|
.iter()
|
||||||
|
.filter_map(|(i, area)| if let Area::Finite(area) = area {
|
||||||
|
Some((i, area))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
})
|
||||||
|
.max_by_key(|(_, area)| *area)
|
||||||
|
.expect("failed to find max area");
|
||||||
|
|
||||||
|
println!("part.one={}", area);
|
||||||
|
|
||||||
|
let mut grid_view = vec!['.'; width * height].into_boxed_slice();
|
||||||
|
|
||||||
|
for y in 0..height {
|
||||||
|
for x in 0..width {
|
||||||
|
let total = coordinates
|
||||||
|
.iter()
|
||||||
|
.map(|c| distance(*c, (x as i32, y as i32)))
|
||||||
|
.sum::<i32>();
|
||||||
|
|
||||||
|
if total < 10_000 {
|
||||||
|
grid_view[x + (y * width)] = '#';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let area = grid_view.iter().filter(|r| **r == '#').count();
|
||||||
|
|
||||||
|
println!("part.two={}", area);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user