From 9b09b974ebc8773444b93843a3a3c6d43384867b Mon Sep 17 00:00:00 2001 From: Stefan Schwarz Date: Wed, 13 May 2020 00:53:12 +0200 Subject: [PATCH] remove non-astroids from map --- src/day10/main.rs | 75 +++++++++++++++++++---------------------------- 1 file changed, 30 insertions(+), 45 deletions(-) diff --git a/src/day10/main.rs b/src/day10/main.rs index 9366ac0..2269e31 100644 --- a/src/day10/main.rs +++ b/src/day10/main.rs @@ -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 { - 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, + map: Vec, } impl MonitoringStation { fn laser(&self, root: &XY, skip: usize, n: usize) -> Vec { - let mut map: HashMap> = HashMap::new(); + let mut map: HashMap> = 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::>(); - 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::>() } 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 = 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::>() }) - .fold(map, |mut map, (xy, p)| { - map.insert(xy.clone(), p.clone()); - map - }); + .collect::>(); MonitoringStation { map } } }