Move manhattan metric function to trait.

This commit is contained in:
2018-12-22 09:40:09 +01:00
parent ba321caa37
commit 3b825ac933
5 changed files with 53 additions and 38 deletions

View File

@@ -32,10 +32,10 @@ z
<defs> <defs>
<path d="M 0 0 <path d="M 0 0
L 0 3.5 L 0 3.5
" id="mfbab4e6f80" style="stroke:#000000;stroke-width:0.8;"/> " id="me9ddb36467" style="stroke:#000000;stroke-width:0.8;"/>
</defs> </defs>
<g> <g>
<use style="stroke:#000000;stroke-width:0.8;" x="60.305455" xlink:href="#mfbab4e6f80" y="307.584"/> <use style="stroke:#000000;stroke-width:0.8;" x="60.305455" xlink:href="#me9ddb36467" y="307.584"/>
</g> </g>
</g> </g>
<g id="text_1"> <g id="text_1">
@@ -71,7 +71,7 @@ z
<g id="xtick_2"> <g id="xtick_2">
<g id="line2d_2"> <g id="line2d_2">
<g> <g>
<use style="stroke:#000000;stroke-width:0.8;" x="127.941818" xlink:href="#mfbab4e6f80" y="307.584"/> <use style="stroke:#000000;stroke-width:0.8;" x="127.941818" xlink:href="#me9ddb36467" y="307.584"/>
</g> </g>
</g> </g>
<g id="text_2"> <g id="text_2">
@@ -110,7 +110,7 @@ z
<g id="xtick_3"> <g id="xtick_3">
<g id="line2d_3"> <g id="line2d_3">
<g> <g>
<use style="stroke:#000000;stroke-width:0.8;" x="195.578182" xlink:href="#mfbab4e6f80" y="307.584"/> <use style="stroke:#000000;stroke-width:0.8;" x="195.578182" xlink:href="#me9ddb36467" y="307.584"/>
</g> </g>
</g> </g>
<g id="text_3"> <g id="text_3">
@@ -139,7 +139,7 @@ z
<g id="xtick_4"> <g id="xtick_4">
<g id="line2d_4"> <g id="line2d_4">
<g> <g>
<use style="stroke:#000000;stroke-width:0.8;" x="263.214545" xlink:href="#mfbab4e6f80" y="307.584"/> <use style="stroke:#000000;stroke-width:0.8;" x="263.214545" xlink:href="#me9ddb36467" y="307.584"/>
</g> </g>
</g> </g>
<g id="text_4"> <g id="text_4">
@@ -153,7 +153,7 @@ z
<g id="xtick_5"> <g id="xtick_5">
<g id="line2d_5"> <g id="line2d_5">
<g> <g>
<use style="stroke:#000000;stroke-width:0.8;" x="330.850909" xlink:href="#mfbab4e6f80" y="307.584"/> <use style="stroke:#000000;stroke-width:0.8;" x="330.850909" xlink:href="#me9ddb36467" y="307.584"/>
</g> </g>
</g> </g>
<g id="text_5"> <g id="text_5">
@@ -193,7 +193,7 @@ z
<g id="xtick_6"> <g id="xtick_6">
<g id="line2d_6"> <g id="line2d_6">
<g> <g>
<use style="stroke:#000000;stroke-width:0.8;" x="398.487273" xlink:href="#mfbab4e6f80" y="307.584"/> <use style="stroke:#000000;stroke-width:0.8;" x="398.487273" xlink:href="#me9ddb36467" y="307.584"/>
</g> </g>
</g> </g>
<g id="text_6"> <g id="text_6">
@@ -286,10 +286,10 @@ z
<defs> <defs>
<path d="M 0 0 <path d="M 0 0
L -3.5 0 L -3.5 0
" id="m57cd4cc848" style="stroke:#000000;stroke-width:0.8;"/> " id="mb89a9b780e" style="stroke:#000000;stroke-width:0.8;"/>
</defs> </defs>
<g> <g>
<use style="stroke:#000000;stroke-width:0.8;" x="57.6" xlink:href="#m57cd4cc848" y="295.488"/> <use style="stroke:#000000;stroke-width:0.8;" x="57.6" xlink:href="#mb89a9b780e" y="295.488"/>
</g> </g>
</g> </g>
<g id="text_8"> <g id="text_8">
@@ -302,7 +302,7 @@ L -3.5 0
<g id="ytick_2"> <g id="ytick_2">
<g id="line2d_8"> <g id="line2d_8">
<g> <g>
<use style="stroke:#000000;stroke-width:0.8;" x="57.6" xlink:href="#m57cd4cc848" y="247.488"/> <use style="stroke:#000000;stroke-width:0.8;" x="57.6" xlink:href="#mb89a9b780e" y="247.488"/>
</g> </g>
</g> </g>
<g id="text_9"> <g id="text_9">
@@ -316,7 +316,7 @@ L -3.5 0
<g id="ytick_3"> <g id="ytick_3">
<g id="line2d_9"> <g id="line2d_9">
<g> <g>
<use style="stroke:#000000;stroke-width:0.8;" x="57.6" xlink:href="#m57cd4cc848" y="199.488"/> <use style="stroke:#000000;stroke-width:0.8;" x="57.6" xlink:href="#mb89a9b780e" y="199.488"/>
</g> </g>
</g> </g>
<g id="text_10"> <g id="text_10">
@@ -331,7 +331,7 @@ L -3.5 0
<g id="ytick_4"> <g id="ytick_4">
<g id="line2d_10"> <g id="line2d_10">
<g> <g>
<use style="stroke:#000000;stroke-width:0.8;" x="57.6" xlink:href="#m57cd4cc848" y="151.488"/> <use style="stroke:#000000;stroke-width:0.8;" x="57.6" xlink:href="#mb89a9b780e" y="151.488"/>
</g> </g>
</g> </g>
<g id="text_11"> <g id="text_11">
@@ -346,7 +346,7 @@ L -3.5 0
<g id="ytick_5"> <g id="ytick_5">
<g id="line2d_11"> <g id="line2d_11">
<g> <g>
<use style="stroke:#000000;stroke-width:0.8;" x="57.6" xlink:href="#m57cd4cc848" y="103.488"/> <use style="stroke:#000000;stroke-width:0.8;" x="57.6" xlink:href="#mb89a9b780e" y="103.488"/>
</g> </g>
</g> </g>
<g id="text_12"> <g id="text_12">
@@ -361,7 +361,7 @@ L -3.5 0
<g id="ytick_6"> <g id="ytick_6">
<g id="line2d_12"> <g id="line2d_12">
<g> <g>
<use style="stroke:#000000;stroke-width:0.8;" x="57.6" xlink:href="#m57cd4cc848" y="55.488"/> <use style="stroke:#000000;stroke-width:0.8;" x="57.6" xlink:href="#mb89a9b780e" y="55.488"/>
</g> </g>
</g> </g>
<g id="text_13"> <g id="text_13">
@@ -577,7 +577,7 @@ z
</g> </g>
</g> </g>
<g id="line2d_13"> <g id="line2d_13">
<path clip-path="url(#p9e82416f48)" d="M 73.832727 282.048 <path clip-path="url(#p96aa94e7dc)" d="M 73.832727 282.048
L 87.36 277.248 L 87.36 277.248
L 100.887273 270.528 L 100.887273 270.528
L 114.414545 269.568 L 114.414545 269.568
@@ -605,7 +605,7 @@ L 398.487273 289.728
" style="fill:none;stroke:#1f77b4;stroke-linecap:square;stroke-width:1.5;"/> " style="fill:none;stroke:#1f77b4;stroke-linecap:square;stroke-width:1.5;"/>
</g> </g>
<g id="line2d_14"> <g id="line2d_14">
<path clip-path="url(#p9e82416f48)" d="M 73.832727 295.488 <path clip-path="url(#p96aa94e7dc)" d="M 73.832727 295.488
L 87.36 293.568 L 87.36 293.568
L 100.887273 295.488 L 100.887273 295.488
L 114.414545 295.488 L 114.414545 295.488
@@ -633,7 +633,7 @@ L 398.487273 295.488
" style="fill:none;stroke:#ff7f0e;stroke-linecap:square;stroke-width:1.5;"/> " style="fill:none;stroke:#ff7f0e;stroke-linecap:square;stroke-width:1.5;"/>
</g> </g>
<g id="line2d_15"> <g id="line2d_15">
<path clip-path="url(#p9e82416f48)" d="M 73.832727 232.128 <path clip-path="url(#p96aa94e7dc)" d="M 73.832727 232.128
L 87.36 218.688 L 87.36 218.688
L 100.887273 208.128 L 100.887273 208.128
L 114.414545 191.808 L 114.414545 191.808
@@ -870,7 +870,7 @@ L 348.72625 83.926688
</g> </g>
</g> </g>
<defs> <defs>
<clipPath id="p9e82416f48"> <clipPath id="p96aa94e7dc">
<rect height="266.112" width="357.12" x="57.6" y="41.472"/> <rect height="266.112" width="357.12" x="57.6" y="41.472"/>
</clipPath> </clipPath>
</defs> </defs>

Before

Width:  |  Height:  |  Size: 25 KiB

After

Width:  |  Height:  |  Size: 25 KiB

View File

@@ -1,11 +1,36 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt::Debug;
use std::hash::Hash; use std::hash::Hash;
use std::io; use std::io;
use std::io::Read; use std::io::Read;
use std::str::FromStr;
use std::ops::Add; use std::ops::Add;
use std::ops::Sub; use std::ops::Sub;
use std::fmt::Debug; use std::str::FromStr;
/// Utility trait for things representing coordinates.
///
/// This is implemented by default for any simple pair-tuple
pub trait Point {
type CoordType;
/// Compute the manhattan distance between this and another.
///
/// The distance will always be >= 0.
fn manhattan(self, other: Self) -> Self::CoordType;
}
impl<T> Point for (T, T)
where T: Add<Output=T> + Sub<Output=T> + Copy + Ord
{
type CoordType = T;
fn manhattan(self, other: Self) -> T {
let (xa, ya) = self;
let (xb, yb) = other;
xa.max(xb) + ya.max(yb) - xa.min(xb) - ya.min(yb)
}
}
/// Apply Erathostenes's sieve to the supplied array /// Apply Erathostenes's sieve to the supplied array
/// ///
@@ -68,16 +93,6 @@ pub fn read_single_input<T>(input: &mut Read) -> T
buf.trim().parse().unwrap() buf.trim().parse().unwrap()
} }
/// Compute the manhattan distance between two points of arbitrary type.
pub fn manhattan_distance<T>(a: (T, T), b: (T, T)) -> T
where T: Copy + Add<Output=T> + Sub<Output=T> + Ord
{
let (xa, ya) = a;
let (xb, yb) = b;
xa.max(xb) + ya.max(yb) - xa.min(xb) - ya.min(yb)
}
/// An interface to count elements in particular categories. /// An interface to count elements in particular categories.
pub trait GroupingCount { pub trait GroupingCount {
/// The type of the categories under inspection /// The type of the categories under inspection

View File

@@ -4,7 +4,7 @@ use std::io::BufReader;
use std::io::Read; use std::io::Read;
use common::GroupingCount; use common::GroupingCount;
use common::manhattan_distance; use common::Point;
use common::Solution; use common::Solution;
type Coordinate = (usize, usize); type Coordinate = (usize, usize);
@@ -61,7 +61,7 @@ impl Day06 {
let mut cur_best = None; let mut cur_best = None;
for (i, point) in self.points.iter().enumerate() { for (i, point) in self.points.iter().enumerate() {
let dist = manhattan_distance(*point, (x, y)); let dist = point.manhattan((x, y));
if dist < cur_dist { if dist < cur_dist {
cur_dist = dist; cur_dist = dist;
cur_best = Some(i); cur_best = Some(i);
@@ -82,7 +82,7 @@ impl Day06 {
self.read_points(input); self.read_points(input);
self.range() self.range()
.map(|x| self.points.iter().map(|y| manhattan_distance(x, *y)).sum::<usize>()) .map(|x| self.points.iter().map(|y| y.manhattan(x)).sum::<usize>())
.filter(|&x| x < limit) .filter(|&x| x < limit)
.count() .count()
} }

View File

@@ -6,7 +6,7 @@ use std::io::BufRead;
use std::io::BufReader; use std::io::BufReader;
use std::io::Read; use std::io::Read;
use common::manhattan_distance; use common::Point;
use common::Solution; use common::Solution;
type Coordinate = (usize, usize); type Coordinate = (usize, usize);
@@ -122,7 +122,7 @@ impl Day15 {
let to_attack = self.units.iter() let to_attack = self.units.iter()
.enumerate() .enumerate()
.filter(|(_, x)| x.faction != faction && x.is_alive()) .filter(|(_, x)| x.faction != faction && x.is_alive())
.filter(|(_, x)| manhattan_distance(x.pos, initial) == 1) .filter(|(_, x)| x.pos.manhattan(initial) == 1)
.min_by(|&(_, a), &(_, b)| a.hp.cmp(&b.hp).then(a.pos.cmp(&b.pos))); .min_by(|&(_, a), &(_, b)| a.hp.cmp(&b.hp).then(a.pos.cmp(&b.pos)));
if let Some((index, _)) = to_attack { if let Some((index, _)) = to_attack {

View File

@@ -5,7 +5,7 @@ use std::io::BufRead;
use std::io::BufReader; use std::io::BufReader;
use std::io::Read; use std::io::Read;
use common::manhattan_distance; use common::Point;
use common::Solution; use common::Solution;
type Coordinate = (usize, usize); type Coordinate = (usize, usize);
@@ -107,7 +107,7 @@ impl Solution for Day22 {
let mut visited: HashSet<State> = HashSet::new(); let mut visited: HashSet<State> = HashSet::new();
let target_state = State { pos: target, climbing: false, torch: true }; let target_state = State { pos: target, climbing: false, torch: true };
todo.push((Reverse(manhattan_distance((0, 0), target)), Reverse(0), State { pos: (0, 0), climbing: false, torch: true })); todo.push((Reverse((0, 0).manhattan(target)), Reverse(0), State { pos: (0, 0), climbing: false, torch: true }));
while let Some((Reverse(approx), Reverse(dist), state)) = todo.pop() { while let Some((Reverse(approx), Reverse(dist), state)) = todo.pop() {
if visited.contains(&state) { if visited.contains(&state) {
@@ -147,7 +147,7 @@ impl Solution for Day22 {
}; };
if !visited.contains(&new_state) && new_state.is_valid(table[yn][xn]) && (x == xn || y == yn) { if !visited.contains(&new_state) && new_state.is_valid(table[yn][xn]) && (x == xn || y == yn) {
todo.push((Reverse(dist + 1 + manhattan_distance(target, new_state.pos)), Reverse(dist + 1), new_state)); todo.push((Reverse(dist + 1 + target.manhattan(new_state.pos)), Reverse(dist + 1), new_state));
} }
} }
} }