day06 both
This commit is contained in:
50
input/day06.txt
Normal file
50
input/day06.txt
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
152, 292
|
||||||
|
163, 90
|
||||||
|
258, 65
|
||||||
|
123, 147
|
||||||
|
342, 42
|
||||||
|
325, 185
|
||||||
|
69, 45
|
||||||
|
249, 336
|
||||||
|
92, 134
|
||||||
|
230, 241
|
||||||
|
74, 262
|
||||||
|
241, 78
|
||||||
|
299, 58
|
||||||
|
231, 146
|
||||||
|
239, 87
|
||||||
|
44, 157
|
||||||
|
156, 340
|
||||||
|
227, 226
|
||||||
|
212, 318
|
||||||
|
194, 135
|
||||||
|
235, 146
|
||||||
|
171, 197
|
||||||
|
160, 59
|
||||||
|
218, 205
|
||||||
|
323, 102
|
||||||
|
290, 356
|
||||||
|
244, 214
|
||||||
|
174, 250
|
||||||
|
70, 331
|
||||||
|
288, 80
|
||||||
|
268, 128
|
||||||
|
359, 98
|
||||||
|
78, 249
|
||||||
|
221, 48
|
||||||
|
321, 228
|
||||||
|
52, 225
|
||||||
|
151, 302
|
||||||
|
183, 150
|
||||||
|
142, 327
|
||||||
|
172, 56
|
||||||
|
72, 321
|
||||||
|
225, 298
|
||||||
|
265, 300
|
||||||
|
86, 288
|
||||||
|
78, 120
|
||||||
|
146, 345
|
||||||
|
268, 181
|
||||||
|
243, 235
|
||||||
|
262, 268
|
||||||
|
40, 60
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
fn main() {
|
fn main() {
|
||||||
aoc_2018::tasks::day05::task1();
|
// aoc_2018::tasks::day06::task1();
|
||||||
aoc_2018::tasks::day05::task2();
|
aoc_2018::tasks::day06::task2();
|
||||||
}
|
}
|
||||||
|
|||||||
150
src/tasks/day06.rs
Normal file
150
src/tasks/day06.rs
Normal file
@@ -0,0 +1,150 @@
|
|||||||
|
use utils;
|
||||||
|
|
||||||
|
pub fn task1() {
|
||||||
|
use self::Bla::*;
|
||||||
|
let input = utils::read_file("input/day06.txt");
|
||||||
|
|
||||||
|
let coordinates: Vec<(u16, u16)> = input
|
||||||
|
.lines()
|
||||||
|
.map(|line| {
|
||||||
|
let mut split = line.split(", ");
|
||||||
|
(
|
||||||
|
split.next().unwrap().parse().unwrap(),
|
||||||
|
split.next().unwrap().parse().unwrap(),
|
||||||
|
)
|
||||||
|
}).collect();
|
||||||
|
|
||||||
|
let max_x = coordinates.iter().max_by_key(|it| it.0).unwrap().0;
|
||||||
|
let max_y = coordinates.iter().max_by_key(|it| it.1).unwrap().1;
|
||||||
|
|
||||||
|
let mut area: Vec<Vec<Bla>> = Vec::new();
|
||||||
|
for _ in 0..max_x {
|
||||||
|
let mut vec = Vec::new();
|
||||||
|
for _ in 0..max_y {
|
||||||
|
vec.push(Bla::None);
|
||||||
|
}
|
||||||
|
area.push(vec);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (_, (a, b)) in coordinates.iter().enumerate() {
|
||||||
|
for x in 0..area.len() {
|
||||||
|
for y in 0..area[x].len() {
|
||||||
|
let d = (i32::abs(*a as i32 - x as i32) + i32::abs(*b as i32 - y as i32)) as u16;
|
||||||
|
area[x][y] = match area[x][y] {
|
||||||
|
None => Single(d, (*a, *b)),
|
||||||
|
Single(dd, (aa, bb)) => {
|
||||||
|
if dd < d {
|
||||||
|
Single(dd, (aa, bb))
|
||||||
|
} else if dd > d {
|
||||||
|
Single(d, (*a, *b))
|
||||||
|
} else {
|
||||||
|
// equal
|
||||||
|
Multi(d)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Multi(dd) => {
|
||||||
|
if d < dd {
|
||||||
|
Single(d, (*a, *b))
|
||||||
|
} else {
|
||||||
|
Multi(dd)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let occupation = coordinates
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(_, (a, b))| {
|
||||||
|
let count = area
|
||||||
|
.iter()
|
||||||
|
.flat_map(|v| v)
|
||||||
|
.filter(|entry| {
|
||||||
|
if let Single(_, (x, y)) = entry {
|
||||||
|
if a == x && b == y {
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}).count();
|
||||||
|
let infinite = area[0].iter().any(|bla| bla.belongs_to_point(*a, *b))
|
||||||
|
|| area[area.len() - 1]
|
||||||
|
.iter()
|
||||||
|
.any(|bla| bla.belongs_to_point(*a, *b))
|
||||||
|
|| area.iter().any(|line| line[0].belongs_to_point(*a, *b))
|
||||||
|
|| area
|
||||||
|
.iter()
|
||||||
|
.any(|line| line[line.len() - 1].belongs_to_point(*a, *b));
|
||||||
|
// println!("{} has size {} (infinite: {:?})", i, count, infinite);
|
||||||
|
(count, infinite)
|
||||||
|
}).collect::<Vec<_>>();
|
||||||
|
|
||||||
|
println!(
|
||||||
|
"Overall occupation: {} of {}",
|
||||||
|
occupation.iter().map(|(count, _)| *count).sum::<usize>(),
|
||||||
|
area.len() * area[0].len()
|
||||||
|
);
|
||||||
|
|
||||||
|
let result = occupation
|
||||||
|
.iter()
|
||||||
|
.filter(|(_, infinite)| !infinite)
|
||||||
|
.max_by_key(|x| x.0);
|
||||||
|
|
||||||
|
println!("{:?}", result); // 5224 too high
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
enum Bla {
|
||||||
|
None,
|
||||||
|
/// distance, node
|
||||||
|
Single(u16, (u16, u16)),
|
||||||
|
/// distance
|
||||||
|
Multi(u16),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Bla {
|
||||||
|
fn belongs_to_point(&self, x: u16, y: u16) -> bool {
|
||||||
|
match self {
|
||||||
|
Bla::Single(_, (a, b)) => *a == x && *b == y,
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn task2() {
|
||||||
|
let input = utils::read_file("input/day06.txt");
|
||||||
|
|
||||||
|
let coordinates: Vec<(u16, u16)> = input
|
||||||
|
.lines()
|
||||||
|
.map(|line| {
|
||||||
|
let mut split = line.split(", ");
|
||||||
|
(
|
||||||
|
split.next().unwrap().parse().unwrap(),
|
||||||
|
split.next().unwrap().parse().unwrap(),
|
||||||
|
)
|
||||||
|
}).collect();
|
||||||
|
|
||||||
|
let max_x = coordinates.iter().max_by_key(|it| it.0).unwrap().0;
|
||||||
|
let max_y = coordinates.iter().max_by_key(|it| it.1).unwrap().1;
|
||||||
|
|
||||||
|
let result: usize = (0..max_x)
|
||||||
|
.map(|x| {
|
||||||
|
(0..max_y)
|
||||||
|
.map(|y| {
|
||||||
|
coordinates
|
||||||
|
.iter()
|
||||||
|
.map(|(a, b)| {
|
||||||
|
(i32::abs(*a as i32 - x as i32) + i32::abs(*b as i32 - y as i32))
|
||||||
|
as usize
|
||||||
|
}).sum::<usize>()
|
||||||
|
}).filter(|it| *it < 10000)
|
||||||
|
.count()
|
||||||
|
}).sum();
|
||||||
|
|
||||||
|
println!("Part 2: {}", result);
|
||||||
|
}
|
||||||
@@ -3,3 +3,4 @@ pub mod day02;
|
|||||||
pub mod day03;
|
pub mod day03;
|
||||||
pub mod day04;
|
pub mod day04;
|
||||||
pub mod day05;
|
pub mod day05;
|
||||||
|
pub mod day06;
|
||||||
|
|||||||
Reference in New Issue
Block a user