@@ -1,3 +1,5 @@
|
||||
use std::cmp::Ordering::{Equal, Greater, Less};
|
||||
|
||||
use crate::utils;
|
||||
|
||||
pub fn task1() {
|
||||
@@ -12,7 +14,8 @@ pub fn task1() {
|
||||
split.next().unwrap().parse().unwrap(),
|
||||
split.next().unwrap().parse().unwrap(),
|
||||
)
|
||||
}).collect();
|
||||
})
|
||||
.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;
|
||||
@@ -26,22 +29,17 @@ pub fn task1() {
|
||||
area.push(vec);
|
||||
}
|
||||
|
||||
for (_, (a, b)) in coordinates.iter().enumerate() {
|
||||
for x in 0..area.len() {
|
||||
for y in 0..area[x].len() {
|
||||
for (a, b) in coordinates.iter() {
|
||||
for (x, col) in area.iter_mut().enumerate() {
|
||||
for (y, cell) in col.iter_mut().enumerate() {
|
||||
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] {
|
||||
*cell = match *cell {
|
||||
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)
|
||||
}
|
||||
}
|
||||
Single(dd, (aa, bb)) => match dd.cmp(&d) {
|
||||
Less => Single(dd, (aa, bb)),
|
||||
Equal => Single(d, (*a, *b)),
|
||||
Greater => Multi(d),
|
||||
},
|
||||
Multi(dd) => {
|
||||
if d < dd {
|
||||
Single(d, (*a, *b))
|
||||
@@ -60,18 +58,15 @@ pub fn task1() {
|
||||
.map(|(_, (a, b))| {
|
||||
let count = area
|
||||
.iter()
|
||||
.flat_map(|v| v)
|
||||
.flatten()
|
||||
.filter(|entry| {
|
||||
if let Single(_, (x, y)) = entry {
|
||||
if a == x && b == y {
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
a == x && b == y
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}).count();
|
||||
})
|
||||
.count();
|
||||
let infinite = area[0].iter().any(|bla| bla.belongs_to_point(*a, *b))
|
||||
|| area[area.len() - 1]
|
||||
.iter()
|
||||
@@ -82,7 +77,8 @@ pub fn task1() {
|
||||
.any(|line| line[line.len() - 1].belongs_to_point(*a, *b));
|
||||
// println!("{} has size {} (infinite: {:?})", i, count, infinite);
|
||||
(count, infinite)
|
||||
}).collect::<Vec<_>>();
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
println!(
|
||||
"Overall occupation: {} of {}",
|
||||
@@ -127,7 +123,8 @@ pub fn task2() {
|
||||
split.next().unwrap().parse().unwrap(),
|
||||
split.next().unwrap().parse().unwrap(),
|
||||
)
|
||||
}).collect();
|
||||
})
|
||||
.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;
|
||||
@@ -141,10 +138,13 @@ pub fn task2() {
|
||||
.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)
|
||||
})
|
||||
.sum::<usize>()
|
||||
})
|
||||
.filter(|it| *it < 10000)
|
||||
.count()
|
||||
}).sum();
|
||||
})
|
||||
.sum();
|
||||
|
||||
println!("Part 2: {}", result);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user