remove non-astroids from map

This commit is contained in:
Stefan Schwarz 2020-05-13 00:53:12 +02:00
parent f12c16a06a
commit 9b09b974eb

View file

@ -1,4 +1,3 @@
use std::cmp::Ordering;
use std::collections::{HashMap, HashSet};
use std::fs::File;
use std::io::{BufRead, BufReader};
@ -46,18 +45,6 @@ impl Sub for &XY {
}
}
impl PartialOrd for XY {
fn partial_cmp(&self, other: &XY) -> Option<Ordering> {
self.angle().partial_cmp(&other.angle())
}
}
impl Ord for XY {
fn cmp(&self, other: &XY) -> Ordering {
self.angle().partial_cmp(&other.angle()).unwrap()
}
}
impl XY {
fn direction(&self) -> Self {
let d = gcd(self.y, self.x).abs();
@ -88,20 +75,17 @@ impl XY {
}
struct MonitoringStation {
map: HashMap<XY, Position>,
map: Vec<XY>,
}
impl MonitoringStation {
fn laser(&self, root: &XY, skip: usize, n: usize) -> Vec<XY> {
let mut map: HashMap<XY, Vec<XY>> = HashMap::new();
let mut map: HashMap<XY, Vec<&XY>> = HashMap::new();
// collect vec for each direction
map = self
.map
.iter()
.filter_map(|(xy, pos)| {
if *pos == Position::Empty {
return None;
}
.filter_map(|xy| {
if xy == root {
return None;
}
@ -109,7 +93,7 @@ impl MonitoringStation {
Some((xy, rel.direction()))
})
.fold(map, |mut acc, (xy, dir)| {
acc.entry(dir).or_insert(vec![]).push(xy.clone());
acc.entry(dir).or_insert(vec![]).push(xy);
acc
});
@ -122,8 +106,9 @@ impl MonitoringStation {
// lets iterate over sorted angles
let mut angles = map.keys().cloned().collect::<Vec<XY>>();
angles.sort();
angles.sort_by(|a, b| a.angle().partial_cmp(&b.angle()).unwrap());
// cycle around and return first element of each vec
angles
.iter()
.cycle()
@ -140,23 +125,21 @@ impl MonitoringStation {
.flatten()
.skip(skip)
.take(n)
.cloned()
.collect::<Vec<XY>>()
}
fn find(&self) -> (usize, XY) {
let best = (0, XY::from((0, 0)));
self.map
let best = (0, &XY::from((0, 0)));
let best = self
.map
.iter()
.filter(|(_, pos)| **pos == Position::Astroid)
.map(|(root, _)| {
.map(|root| {
let insight = self
.map
.iter()
// get direction to any of the astroids
.filter_map(|(xy, pos)| {
if *pos == Position::Empty {
return None;
}
.filter_map(|xy| {
let rel = xy - root;
Some(rel.direction())
})
@ -169,12 +152,17 @@ impl MonitoringStation {
- 1;
(insight, root)
})
.fold(best, |acc, astroid| {
if astroid.0 > acc.0 {
return (astroid.0, astroid.1.clone());
}
acc
})
.fold(
best,
|acc, astroid| {
if astroid.0 > acc.0 {
astroid
} else {
acc
}
},
);
(best.0, best.1.clone())
}
}
@ -183,25 +171,22 @@ where
T: BufRead,
{
fn from(reader: T) -> Self {
let map: HashMap<XY, Position> = HashMap::new();
let map = reader
.split(b'\n')
.enumerate()
.flat_map(|(y, line)| {
line.unwrap()
.iter()
.into_iter()
.enumerate()
.map(|(x, c)| {
.filter_map(move |(x, c)| {
let xy = XY::from((x, y));
let p = Position::from(c);
(xy, p)
match Position::from(&c) {
Position::Astroid => Some(xy),
Position::Empty => None,
}
})
.collect::<Vec<(XY, Position)>>()
})
.fold(map, |mut map, (xy, p)| {
map.insert(xy.clone(), p.clone());
map
});
.collect::<Vec<XY>>();
MonitoringStation { map }
}
}