Day 17
Feelings
Well.. Part 2 ended up being a complete copy-paste of part 1, but with adding one more dimension. Should generalise neighbor searches somehow.
Learnings
- Learned itertools.multi_cartesian_product
- And implemented my very first actually useful macro (see below)
- Maybe I'll be able to convert this to generic type instead some day, but that day is not today
.. later same day
pub trait VecNeighbors<T> {
fn neighbors(&self) -> Vec<Vec<T>>;
}
macro_rules! VecNeighbor_for {
($vectype:ty) => {
impl VecNeighbors<$vectype> for Vec<$vectype> {
fn neighbors(&self) -> Vec<Vec<$vectype>> {
self
.iter()
.map(|v|(v-1 ..= v+1).into_iter())
.multi_cartesian_product()
.filter(|x| x != self)
.collect::<Vec<_>>()
}
}
};
}
Implement it:
VecNeighbor_for!(i32);
.. and then just use it:
fn test_vecneighbor() {
let _coord:Vec<i32> = vec![0,0];
for x in _coord.neighbors().into_iter() {
println!("{:?}", &x);
}
}
Which gives you this:
[-1, -1]
[-1, 0]
[-1, 1]
[0, -1]
[0, 1]
[1, -1]
[1, 0]
[1, 1]