day12
This commit is contained in:
parent
f12c16a06a
commit
df219182e8
2 changed files with 127 additions and 1 deletions
|
@ -36,4 +36,8 @@ path = "src/day9/main.rs"
|
|||
|
||||
[[bin]]
|
||||
name = "day10"
|
||||
path = "src/day10/main.rs"
|
||||
path = "src/day10/main.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "day12"
|
||||
path = "src/day12/main.rs"
|
||||
|
|
122
src/day12/main.rs
Normal file
122
src/day12/main.rs
Normal file
|
@ -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<Object>,
|
||||
velocy: Vec<Object>,
|
||||
}
|
||||
|
||||
impl From<Vec<Object>> for Space {
|
||||
fn from(moons: Vec<Object>) -> Self {
|
||||
let velocy = Vec::from_iter((0..moons.len()).map(|_| Object::default()));
|
||||
Space { moons, velocy }
|
||||
}
|
||||
}
|
||||
|
||||
impl Space {
|
||||
fn step(&mut self) -> impl Iterator<Item = (&'_ Object, &'_ Object, usize)> {
|
||||
let moons: &mut Vec<Object> = self.moons.as_mut();
|
||||
let velocy: &mut Vec<Object> = 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)
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue