day06 both
This commit is contained in:
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);
|
||||
}
|
||||
Reference in New Issue
Block a user