diff --git a/2019/src/bin/day12.rs b/2019/src/bin/day12.rs index 5d2fe1d..03a51b3 100644 --- a/2019/src/bin/day12.rs +++ b/2019/src/bin/day12.rs @@ -1,16 +1,17 @@ use itertools::Itertools; use regex::Regex; +use std::collections::HashSet; use std::str::FromStr; use std::{fs, io, ops}; -#[derive(Debug, Copy, Clone)] +#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq)] struct Point3D { - x: i64, - y: i64, - z: i64, + x: i32, + y: i32, + z: i32, } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Eq, PartialEq, Hash)] struct Moon { position: Point3D, velocity: Point3D, @@ -25,9 +26,36 @@ fn main() -> io::Result<()> { let system: Vec<_> = moons.into_iter().map(|moon| moon.unwrap()).collect(); println!("Part 1: {}", total_energy(&system)); + let moons = parse_positions(&fs::read_to_string("input/day12.txt")?)?; + let cycles = cycles(moons); + + dbg!(cycles); + Ok(()) } +fn cycles(mut moons: Vec>) -> Vec { + let mut cycles = Vec::with_capacity(moons.len()); + for i in 0..moons.len() { + let mut seen = HashSet::new(); + let mut count = 0; + loop { + if count % 1000000 == 0 { + println!("{}", count); + } + if seen.contains(&moons[i]) { + dbg!(&moons[1]); + break; + } + seen.insert(moons[i].clone()); + step(&mut moons); + count += 1; + } + cycles.push(count); + } + cycles +} + fn parse_positions(s: &str) -> io::Result>> { let positions: Vec = s .lines() @@ -76,7 +104,7 @@ fn step(system: &mut Vec>) { } } -fn total_energy(system: &[Moon]) -> i64 { +fn total_energy(system: &[Moon]) -> i32 { system.iter().map(|moon| moon.total_energy()).sum() } @@ -118,15 +146,15 @@ impl Moon { // A moon's potential energy is the sum of the absolute values of its x, y, and z position // coordinates. A moon's kinetic energy is the sum of the absolute values of its velocity // coordinates. - fn potential_energy(&self) -> i64 { + fn potential_energy(&self) -> i32 { self.position.x.abs() + self.position.y.abs() + self.position.z.abs() } - fn kinetic_energy(&self) -> i64 { + fn kinetic_energy(&self) -> i32 { self.velocity.x.abs() + self.velocity.y.abs() + self.velocity.z.abs() } - fn total_energy(&self) -> i64 { + fn total_energy(&self) -> i32 { self.potential_energy() * self.kinetic_energy() } } @@ -169,4 +197,15 @@ mod tests { step(&mut moons); dbg!(moons); } + + #[test] + fn test_part2() { + let input = " + + + +"; + let moons = parse_positions(input).unwrap(); + dbg!(cycles(moons)); + } }