From df219182e87d28e19b74ef0b32143fb51a4bed02 Mon Sep 17 00:00:00 2001 From: Stefan Schwarz Date: Thu, 14 May 2020 23:35:15 +0200 Subject: [PATCH] day12 --- Cargo.toml | 6 ++- src/day12/main.rs | 122 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 src/day12/main.rs diff --git a/Cargo.toml b/Cargo.toml index 3ee5431..719d289 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,4 +36,8 @@ path = "src/day9/main.rs" [[bin]] name = "day10" -path = "src/day10/main.rs" \ No newline at end of file +path = "src/day10/main.rs" + +[[bin]] +name = "day12" +path = "src/day12/main.rs" diff --git a/src/day12/main.rs b/src/day12/main.rs new file mode 100644 index 0000000..1cef6c0 --- /dev/null +++ b/src/day12/main.rs @@ -0,0 +1,122 @@ +use std::cmp::Ordering; +use std::iter::FromIterator; + +#[derive(PartialEq, Debug, Default)] +struct Object { + x: isize, + y: isize, + z: isize, +} + +impl From<(isize, isize, isize)> for Object { + fn from(o: (isize, isize, isize)) -> Self { + Object { + x: o.0, + y: o.1, + z: o.2, + } + } +} + +impl Object { + fn attract(&self, other: &Object, v: &mut Object) { + v.x += Object::cmp(self.x, other.x); + v.y += Object::cmp(self.y, other.y); + v.z += Object::cmp(self.z, other.z); + } + + fn apply(&mut self, v: &Object) { + self.x += v.x; + self.y += v.y; + self.z += v.z; + } + + fn cmp(a: isize, b: isize) -> isize { + match b.cmp(&a) { + Ordering::Less => -1, + Ordering::Equal => 0, + Ordering::Greater => 1, + } + } + + fn energy(&self) -> usize { + return (self.x.abs() + self.y.abs() + self.z.abs()) as usize; + } +} + +struct Space { + moons: Vec, + velocy: Vec, +} + +impl From> for Space { + fn from(moons: Vec) -> Self { + let velocy = Vec::from_iter((0..moons.len()).map(|_| Object::default())); + Space { moons, velocy } + } +} + +impl Space { + fn step(&mut self) -> impl Iterator { + let moons: &mut Vec = self.moons.as_mut(); + let velocy: &mut Vec = self.velocy.as_mut(); + + moons.iter().enumerate().for_each(|(idx, a)| { + moons.iter().for_each(|b| { + if a == b { + return; + } + a.attract(b, velocy.get_mut(idx).unwrap()) + }) + }); + moons + .iter_mut() + .enumerate() + .for_each(|(idx, m)| m.apply(velocy.get(idx).unwrap())); + moons + .iter() + .zip(velocy.iter()) + .map(|(a, b)| (a, b, a.energy() * b.energy())) + } +} + +fn main() { + let moons = vec![ + Object::from((-1, 0, 2)), + Object::from((2, -10, -7)), + Object::from((4, -8, 8)), + Object::from((3, 5, -1)), + ]; + let moons = vec![ + Object::from((-8, -10, 0)), + Object::from((5, 5, 10)), + Object::from((2, -7, 3)), + Object::from((9, -8, -3)), + ]; + let moons = vec![ + Object::from((17, 5, 1)), + Object::from((-2, -8, 8)), + Object::from((7, -6, 14)), + Object::from((1, -10, 4)), + ]; + let mut space = Space::from(moons); + + let mut total_energy = 0; + (1..=100).for_each(|_i| { + (1..=9).for_each(|_| { + space.step().count(); + }); + let step_energy = space + .step() + .map(|(o, v, e)| { + println!("{:?} {:?}", o, v); + e + }) + .fold(0, |mut acc, c| { + acc += c; + acc + }); + total_energy += step_energy; + println!("total:{}, stepe:{}", total_energy, step_energy) + }) +}