Commit c0940534 authored by Marcel Behrmann's avatar Marcel Behrmann

day(12): part 1 solved

parent 4625d672
3,8,1005,8,328,1106,0,11,0,0,0,104,1,104,0,3,8,102,-1,8,10,1001,10,1,10,4,10,108,0,8,10,4,10,1002,8,1,28,1,1003,10,10,3,8,1002,8,-1,10,101,1,10,10,4,10,108,1,8,10,4,10,102,1,8,54,2,1103,6,10,3,8,1002,8,-1,10,101,1,10,10,4,10,108,0,8,10,4,10,101,0,8,80,3,8,1002,8,-1,10,1001,10,1,10,4,10,108,1,8,10,4,10,1002,8,1,102,3,8,102,-1,8,10,1001,10,1,10,4,10,108,0,8,10,4,10,1001,8,0,124,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,1,10,4,10,1001,8,0,147,1006,0,35,1,7,3,10,2,106,13,10,2,1104,9,10,3,8,102,-1,8,10,1001,10,1,10,4,10,108,0,8,10,4,10,1002,8,1,183,2,7,16,10,2,105,14,10,1,1002,12,10,1006,0,13,3,8,102,-1,8,10,1001,10,1,10,4,10,108,0,8,10,4,10,1002,8,1,220,1006,0,78,2,5,3,10,1006,0,92,1006,0,92,3,8,1002,8,-1,10,101,1,10,10,4,10,108,1,8,10,4,10,1001,8,0,255,1006,0,57,2,1001,11,10,1006,0,34,2,1007,18,10,3,8,1002,8,-1,10,101,1,10,10,4,10,1008,8,1,10,4,10,1002,8,1,292,2,109,3,10,1,1103,14,10,2,2,5,10,2,1006,3,10,101,1,9,9,1007,9,997,10,1005,10,15,99,109,650,104,0,104,1,21101,932700762920,0,1,21101,0,345,0,1105,1,449,21102,1,386577306516,1,21102,356,1,0,1106,0,449,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,21101,179355975827,0,1,21101,403,0,0,1106,0,449,21102,1,46413220903,1,21102,1,414,0,1106,0,449,3,10,104,0,104,0,3,10,104,0,104,0,21101,988224959252,0,1,21102,1,437,0,1106,0,449,21101,717637968660,0,1,21101,0,448,0,1106,0,449,99,109,2,22101,0,-1,1,21102,40,1,2,21101,480,0,3,21101,470,0,0,1106,0,513,109,-2,2105,1,0,0,1,0,0,1,109,2,3,10,204,-1,1001,475,476,491,4,0,1001,475,1,475,108,4,475,10,1006,10,507,1102,1,0,475,109,-2,2105,1,0,0,109,4,2102,1,-1,512,1207,-3,0,10,1006,10,530,21102,1,0,-3,22102,1,-3,1,22101,0,-2,2,21102,1,1,3,21101,0,549,0,1105,1,554,109,-4,2105,1,0,109,5,1207,-3,1,10,1006,10,577,2207,-4,-2,10,1006,10,577,21202,-4,1,-4,1106,0,645,21202,-4,1,1,21201,-3,-1,2,21202,-2,2,3,21102,1,596,0,1106,0,554,21201,1,0,-4,21101,1,0,-1,2207,-4,-2,10,1006,10,615,21101,0,0,-1,22202,-2,-1,-2,2107,0,-3,10,1006,10,637,21201,-1,0,1,21101,0,637,0,105,1,512,21202,-2,-1,-2,22201,-4,-2,-4,109,-5,2105,1,0
\ No newline at end of file
3,8,1005,8,320,1106,0,11,0,0,0,104,1,104,0,3,8,1002,8,-1,10,101,1,10,10,4,10,1008,8,1,10,4,10,102,1,8,29,2,1005,1,10,1006,0,11,3,8,1002,8,-1,10,101,1,10,10,4,10,108,0,8,10,4,10,102,1,8,57,1,8,15,10,1006,0,79,1,6,3,10,3,8,102,-1,8,10,101,1,10,10,4,10,108,0,8,10,4,10,101,0,8,90,2,103,18,10,1006,0,3,2,105,14,10,3,8,102,-1,8,10,1001,10,1,10,4,10,108,0,8,10,4,10,101,0,8,123,2,9,2,10,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,1,10,4,10,1001,8,0,150,1,2,2,10,2,1009,6,10,1,1006,12,10,1006,0,81,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,1,10,4,10,102,1,8,187,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,0,10,4,10,101,0,8,209,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,1,10,4,10,101,0,8,231,1,1008,11,10,1,1001,4,10,2,1104,18,10,3,8,102,-1,8,10,1001,10,1,10,4,10,108,1,8,10,4,10,1001,8,0,264,1,8,14,10,1006,0,36,3,8,1002,8,-1,10,1001,10,1,10,4,10,108,0,8,10,4,10,101,0,8,293,1006,0,80,1006,0,68,101,1,9,9,1007,9,960,10,1005,10,15,99,109,642,104,0,104,1,21102,1,846914232732,1,21102,1,337,0,1105,1,441,21102,1,387512115980,1,21101,348,0,0,1106,0,441,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,21102,209533824219,1,1,21102,1,395,0,1106,0,441,21101,0,21477985303,1,21102,406,1,0,1106,0,441,3,10,104,0,104,0,3,10,104,0,104,0,21101,868494234468,0,1,21101,429,0,0,1106,0,441,21102,838429471080,1,1,21102,1,440,0,1106,0,441,99,109,2,21201,-1,0,1,21101,0,40,2,21102,472,1,3,21101,0,462,0,1106,0,505,109,-2,2106,0,0,0,1,0,0,1,109,2,3,10,204,-1,1001,467,468,483,4,0,1001,467,1,467,108,4,467,10,1006,10,499,1102,1,0,467,109,-2,2106,0,0,0,109,4,2101,0,-1,504,1207,-3,0,10,1006,10,522,21101,0,0,-3,21202,-3,1,1,22101,0,-2,2,21102,1,1,3,21102,541,1,0,1106,0,546,109,-4,2105,1,0,109,5,1207,-3,1,10,1006,10,569,2207,-4,-2,10,1006,10,569,22102,1,-4,-4,1105,1,637,22102,1,-4,1,21201,-3,-1,2,21202,-2,2,3,21102,588,1,0,1105,1,546,22101,0,1,-4,21102,1,1,-1,2207,-4,-2,10,1006,10,607,21101,0,0,-1,22202,-2,-1,-2,2107,0,-3,10,1006,10,629,21201,-1,0,1,21102,629,1,0,105,1,504,21202,-2,-1,-2,22201,-4,-2,-4,109,-5,2105,1,0
\ No newline at end of file
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "aho-corasick"
version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "day_12"
version = "0.1.0"
dependencies = [
"regex 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "memchr"
version = "2.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "regex"
version = "1.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"aho-corasick 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"regex-syntax 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)",
"thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "regex-syntax"
version = "0.6.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "thread_local"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[metadata]
"checksum aho-corasick 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)" = "58fb5e95d83b38284460a5fda7d6470aa0b8844d283a0b614b8535e880800d2d"
"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 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"
......@@ -7,3 +7,4 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
regex = "1"
<x=-6, y=-5, z=-8>
<x=0, y=-3, z=-13>
<x=-15, y=10, z=-11>
<x=-3, y=-8, z=3>
<x=-3, y=-8, z=3>
\ No newline at end of file
mod utils;
mod intvm;
mod math;
use std::fmt;
use std::fs;
use std::time::Instant;
use math::Index3;
use regex::Regex;
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");
/*let input =
"<x=-8, y=-10, z=0>
<x=5, y=5, z=10>
<x=2, y=-7, z=3>
<x=9, y=-8, z=-3>";*/
let input = input.lines();
let input: Vec<&str> = input.collect::<Vec<&str>>();
part_01(input.clone());
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());
}
#[allow(unused_variables)]
fn part_01(input: Vec<&str>) {
println!("Result of Part 1");
fn part_01(input: Vec<&str>) -> i64 {
const SIM_LENGTH: i64 = 100000;
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();
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(),
});
}
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() {
if moon == target {
continue;
}
let target = moons[target].clone();
if let Some(moon) = moons.get_mut(moon) {
(*moon).apply_gravity(&target);
}
}
}
//println!("After {} steps", i+1);
for moon in &mut moons {
moon.tick();
//println!("{}", moon);
}
//println!();
}
let mut energy = 0;
for moon in &mut moons {
energy += moon.pos.abs().sum() * moon.vel.abs().sum();
}
energy
}
#[allow(unused_variables)]
......
use std::ops;
use std::fmt;
#[derive(Clone,Debug, PartialEq, PartialOrd)]
pub struct Index3 {
pub x: i64,
pub y: i64,
pub z: i64,
}
impl Index3 {
pub fn abs(&self) -> Index3 {
let mut copy = self.clone();
copy.x = copy.x.abs();
copy.y = copy.y.abs();
copy.z = copy.z.abs();
copy
}
pub fn sum(&self) -> i64 {
self.x + self.y + self.z
}
pub fn zero() -> Index3 {
Index3 {
x: 0,
y: 0,
z: 0,
}
}
}
impl fmt::Display for Index3 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "<x={}, y={}, z={}>", self.x, self.y, self.z)
}
}
impl ops::Add<Index3> for Index3 {
type Output = Index3;
fn add(self, rhs: Index3) -> Index3 {
let mut copy = self.clone();
copy.x += rhs.x;
copy.y += rhs.y;
copy.z += rhs.z;
copy
}
}
impl ops::AddAssign<Index3> for Index3 {
fn add_assign(&mut self, rhs: Index3) -> () {
self.x += rhs.x;
self.y += rhs.y;
self.z += rhs.z;
}
}
impl ops::Mul<i64> for Index3 {
type Output = Index3;
fn mul(self, rhs: i64) -> Index3 {
let mut copy = self.clone();
copy.x *= rhs;
copy.y *= rhs;
copy.z *= rhs;
copy
}
}
pub mod index3;
pub use self::index3::Index3;
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