Commit e5842375 authored by Marcel Behrmann's avatar Marcel Behrmann

day(12): part 2 solved

parent d97b4c19
......@@ -8,10 +8,16 @@ dependencies = [
"memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "autocfg"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "day_12"
version = "0.1.0"
dependencies = [
"num 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
......@@ -25,6 +31,76 @@ name = "memchr"
version = "2.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "num"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"num-bigint 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"num-complex 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"num-integer 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)",
"num-iter 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)",
"num-rational 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num-bigint"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
"num-integer 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num-complex"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num-integer"
version = "0.1.41"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num-iter"
version = "0.1.39"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
"num-integer 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num-rational"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
"num-bigint 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"num-integer 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num-traits"
version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "regex"
version = "1.3.1"
......@@ -51,8 +127,16 @@ dependencies = [
[metadata]
"checksum aho-corasick 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)" = "58fb5e95d83b38284460a5fda7d6470aa0b8844d283a0b614b8535e880800d2d"
"checksum autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "1d49d90015b3c36167a20fe2810c5cd875ad504b39cff3d4eae7977e6b7c1cb2"
"checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
"checksum memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "88579771288728879b57485cc7d6b07d648c9f0141eb955f8ab7f9d45394468e"
"checksum num 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cf4825417e1e1406b3782a8ce92f4d53f26ec055e3622e1881ca8e9f5f9e08db"
"checksum num-bigint 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "f9c3f34cdd24f334cb265d9bf8bfa8a241920d026916785747a92f0e55541a1a"
"checksum num-complex 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "fcb0cf31fb3ff77e6d2a6ebd6800df7fdcd106f2ad89113c9130bcd07f93dffc"
"checksum num-integer 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)" = "b85e541ef8255f6cf42bbfe4ef361305c6c135d10919ecc26126c4e5ae94bc09"
"checksum num-iter 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "76bd5272412d173d6bf9afdf98db8612bbabc9a7a830b7bfc9c188911716132e"
"checksum num-rational 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f2885278d5fe2adc2f75ced642d52d879bffaceb5a2e0b1d4309ffdfb239b454"
"checksum num-traits 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "d4c81ffc11c212fa327657cb19dd85eb7419e163b5b076bede2bdb5c974c07e4"
"checksum regex 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dc220bd33bdce8f093101afe22a037b8eb0e5af33592e6a9caafff0d4cb81cbd"
"checksum regex-syntax 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)" = "11a7e20d1cce64ef2fed88b66d347f88bd9babb82845b2b858f3edbf59a4f716"
"checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b"
......@@ -8,3 +8,4 @@ edition = "2018"
[dependencies]
regex = "1"
num = "*"
mod utils;
mod intvm;
mod math;
mod moon;
mod system;
use std::fmt;
use moon::Moon;
use system::System;
use std::fs;
use std::time::Instant;
use math::Index3;
use regex::Regex;
use std::collections::HashSet;
use num::integer;
const INPUT_FILE: &str = "input.txt";
const REGEX_PATTERN: &str = r"x=(-?\d*), y=(-?\d*), z=(-?\d*)";
#[derive(Debug, PartialEq, PartialOrd, Clone)]
struct Moon {
pos: Index3,
vel: Index3
}
impl fmt::Display for Moon {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "pos={}, vel={}", self.pos, self.vel)
}
}
impl Moon {
fn apply_gravity(&mut self, moon: &Moon) -> () {
if self.pos.x > moon.pos.x {
self.vel.x -= 1;
}
if self.pos.x < moon.pos.x {
self.vel.x += 1;
}
if self.pos.y > moon.pos.y {
self.vel.y -= 1;
}
if self.pos.y < moon.pos.y {
self.vel.y += 1;
}
if self.pos.z > moon.pos.z {
self.vel.z -= 1;
}
if self.pos.z < moon.pos.z {
self.vel.z += 1;
}
}
fn tick(&mut self) -> () {
self.pos += self.vel.clone();
}
}
fn main() {
let input = fs::read_to_string(INPUT_FILE).expect("Error reading input");
......@@ -66,10 +28,8 @@ fn main() {
let input = input.lines();
let input: Vec<&str> = input.collect::<Vec<&str>>();
let now = Instant::now();
println!("Result part 01: {}", part_01(input.clone()));
println!("{} ticks took {}ms", 100000, now.elapsed().as_millis());
part_02(input.clone());
println!("Result part 02: {}", part_02(input.clone()));
}
#[allow(unused_variables)]
......@@ -79,8 +39,6 @@ fn part_01(input: Vec<&str>) -> i64 {
let mut moons: Vec<Moon> = Vec::new();
let re = Regex::new(REGEX_PATTERN).unwrap();
println!("Size of vec: {}", std::mem::size_of_val(&moons));
for row in input {
let capt = re.captures(row).unwrap();
......@@ -94,14 +52,10 @@ fn part_01(input: Vec<&str>) -> i64 {
});
}
println!("After {} steps", 0);
for moon in &mut moons {
moon.tick();
println!("{}", moon);
}
println!();
for i in 0..SIM_LENGTH {
for moon in 0..moons.len() {
for target in 0..moons.len() {
......@@ -116,13 +70,9 @@ fn part_01(input: Vec<&str>) -> i64 {
}
}
//println!("After {} steps", i+1);
for moon in &mut moons {
moon.tick();
//println!("{}", moon);
}
//println!();
}
let mut energy = 0;
......@@ -135,6 +85,61 @@ fn part_01(input: Vec<&str>) -> i64 {
}
#[allow(unused_variables)]
fn part_02(input: Vec<&str>) {
println!("Result of Part 2");
fn part_02(input: Vec<&str>) -> i64 {
let mut system = System::from(&input);
let mut x_hashes: HashSet<String> = HashSet::new();
let mut y_hashes: HashSet<String> = HashSet::new();
let mut z_hashes: HashSet<String> = HashSet::new();
let mut x_found = false;
let mut y_found = false;
let mut z_found = false;
let mut x = 0;
let mut y = 0;
let mut z = 0;
while !x_found || !y_found {
system.tick();
if !x_found {
let x_hash = system.get_x_hash();
if x_hashes.contains(&x_hash) {
x_found = true;
x = x_hashes.len();
} else {
x_hashes.insert(x_hash);
}
}
if !y_found {
let y_hash = system.get_y_hash();
if y_hashes.contains(&y_hash) {
y_found = true;
y = y_hashes.len();
} else {
y_hashes.insert(y_hash);
}
}
if !z_found {
let z_hash = system.get_z_hash();
if z_hashes.contains(&z_hash) {
z_found = true;
z = z_hashes.len();
} else {
z_hashes.insert(z_hash);
}
}
};
let t = integer::lcm(x, y);
let t = integer::lcm(t, z);
t as i64
}
\ No newline at end of file
use std::fmt;
use crate::math::Index3;
#[derive(Debug, PartialEq, PartialOrd, Clone)]
pub struct Moon {
pub pos: Index3,
pub vel: Index3
}
impl fmt::Display for Moon {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "pos={}, vel={}", self.pos, self.vel)
}
}
impl Moon {
pub fn apply_gravity(&mut self, moon: &Moon) -> () {
if self.pos.x > moon.pos.x {
self.vel.x -= 1;
}
if self.pos.x < moon.pos.x {
self.vel.x += 1;
}
if self.pos.y > moon.pos.y {
self.vel.y -= 1;
}
if self.pos.y < moon.pos.y {
self.vel.y += 1;
}
if self.pos.z > moon.pos.z {
self.vel.z -= 1;
}
if self.pos.z < moon.pos.z {
self.vel.z += 1;
}
}
pub fn tick(&mut self) -> () {
self.pos += self.vel.clone();
}
}
\ No newline at end of file
use regex::Regex;
use crate::math::Index3;
use crate::moon::Moon;
const REGEX_PATTERN: &str = r"x=(-?\d*), y=(-?\d*), z=(-?\d*)";
pub struct System {
pub moons: Vec<Moon>,
}
impl System {
pub fn from(input: &Vec<&str>) -> System {
let re = Regex::new(REGEX_PATTERN).unwrap();
let mut sys = System {
moons: Vec::new(),
};
for row in input {
let capt = re.captures(row).unwrap();
sys.moons.push(Moon {
pos: Index3 {
x: capt.get(1).unwrap().as_str().parse::<i64>().unwrap(),
y: capt.get(2).unwrap().as_str().parse::<i64>().unwrap(),
z: capt.get(3).unwrap().as_str().parse::<i64>().unwrap(),
},
vel: Index3::zero(),
});
}
sys
}
pub fn tick(&mut self) -> () {
for moon in 0..self.moons.len() {
for target in 0..self.moons.len() {
if moon == target {
continue;
}
let target = self.moons[target].clone();
if let Some(moon) = self.moons.get_mut(moon) {
(*moon).apply_gravity(&target);
}
}
}
for moon in &mut self.moons {
moon.tick();
}
}
pub fn get_x_hash(&self) -> String {
let mut hash = String::new();
for moon in &self.moons {
hash.push_str(&moon.pos.x.to_string());
hash.push_str(&moon.vel.x.to_string());
}
hash
}
pub fn get_y_hash(&self) -> String {
let mut hash = String::new();
for moon in &self.moons {
hash.push_str(&moon.pos.y.to_string());
hash.push_str(&moon.vel.y.to_string());
}
hash
}
pub fn get_z_hash(&self) -> String {
let mut hash = String::new();
for moon in &self.moons {
hash.push_str(&moon.pos.z.to_string());
hash.push_str(&moon.vel.z.to_string());
}
hash
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment