day12 task 1
This commit is contained in:
49
Cargo.lock
generated
49
Cargo.lock
generated
@@ -1,10 +1,19 @@
|
|||||||
# This file is automatically @generated by Cargo.
|
# This file is automatically @generated by Cargo.
|
||||||
# It is not intended for manual editing.
|
# 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]]
|
[[package]]
|
||||||
name = "aoc_2019"
|
name = "aoc_2019"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"itertools 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
"itertools 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"regex 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -20,6 +29,46 @@ dependencies = [
|
|||||||
"either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
"either 1.5.3 (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]
|
[metadata]
|
||||||
|
"checksum aho-corasick 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)" = "58fb5e95d83b38284460a5fda7d6470aa0b8844d283a0b614b8535e880800d2d"
|
||||||
"checksum either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3"
|
"checksum either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3"
|
||||||
"checksum itertools 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f56a2d0bc861f9165be4eb3442afd3c236d8a98afd426f65d92324ae1091a484"
|
"checksum itertools 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f56a2d0bc861f9165be4eb3442afd3c236d8a98afd426f65d92324ae1091a484"
|
||||||
|
"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"
|
||||||
|
|||||||
@@ -8,3 +8,4 @@ edition = "2018"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
itertools = "0.8.2"
|
itertools = "0.8.2"
|
||||||
|
regex = "1.3.1"
|
||||||
|
|||||||
4
input/day12.txt
Normal file
4
input/day12.txt
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
<x=19, y=-10, z=7>
|
||||||
|
<x=1, y=2, z=-3>
|
||||||
|
<x=14, y=-4, z=1>
|
||||||
|
<x=8, y=7, z=-6>
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
mod tasks;
|
mod tasks;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
tasks::day11::run();
|
tasks::day12::run();
|
||||||
}
|
}
|
||||||
|
|||||||
101
src/tasks/day12.rs
Normal file
101
src/tasks/day12.rs
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
use regex::Regex;
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn run() {
|
||||||
|
let regex = Regex::new("<x=(-?\\d+),\\sy=(-?\\d+),\\sz=(-?\\d+)").unwrap();
|
||||||
|
let input: Vec<Point> = std::fs::read_to_string("input/day12.txt")
|
||||||
|
.unwrap()
|
||||||
|
.lines()
|
||||||
|
.map(|line| {
|
||||||
|
let m = regex.captures(&line).unwrap();
|
||||||
|
let x: i32 = m[1].parse().unwrap();
|
||||||
|
let y: i32 = m[2].parse().unwrap();
|
||||||
|
let z: i32 = m[3].parse().unwrap();
|
||||||
|
Point { x, y, z }
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
task1(input.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn task1(input: Vec<Point>) {
|
||||||
|
let moons: Vec<_> = input
|
||||||
|
.into_iter()
|
||||||
|
.map(|pos| Moon {
|
||||||
|
pos: pos,
|
||||||
|
vel: Point::new(0, 0, 0),
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
let finals = (0..1000).fold(moons, |moons, _run| {
|
||||||
|
let gravities: Vec<Point> = moons
|
||||||
|
.iter()
|
||||||
|
.map(|base| {
|
||||||
|
moons.iter().fold(Point::new(0, 0, 0), |grav, other_moon| {
|
||||||
|
grav.add(base.pos.gravity(other_moon.pos))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
let moons = moons
|
||||||
|
.into_iter()
|
||||||
|
.zip(gravities.into_iter())
|
||||||
|
.map(|(mut moon, grav)| {
|
||||||
|
moon.vel = moon.vel.add(grav);
|
||||||
|
moon.pos = moon.pos.add(moon.vel);
|
||||||
|
moon
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
//println!("{}: {:?}", run, moons);
|
||||||
|
moons
|
||||||
|
});
|
||||||
|
let energy: i32 = finals
|
||||||
|
.iter()
|
||||||
|
.map(|moon| moon.pos.energy() * moon.vel.energy())
|
||||||
|
.sum();
|
||||||
|
println!("Task 1: sum of energy is {}", energy);
|
||||||
|
//392 too low
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Moon {
|
||||||
|
pos: Point,
|
||||||
|
vel: Point,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
struct Point {
|
||||||
|
x: i32,
|
||||||
|
y: i32,
|
||||||
|
z: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Point {
|
||||||
|
fn new(x: i32, y: i32, z: i32) -> Self {
|
||||||
|
Point { x, y, z }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add(&self, other: Self) -> Self {
|
||||||
|
Point {
|
||||||
|
x: self.x + other.x,
|
||||||
|
y: self.y + other.y,
|
||||||
|
z: self.z + other.z,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn gravity(&self, other: Self) -> Self {
|
||||||
|
fn d(a: i32, b: i32) -> i32 {
|
||||||
|
use std::cmp::Ordering;
|
||||||
|
match a.cmp(&b) {
|
||||||
|
Ordering::Less => 1,
|
||||||
|
Ordering::Equal => 0,
|
||||||
|
Ordering::Greater => -1,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Point {
|
||||||
|
x: d(self.x, other.x),
|
||||||
|
y: d(self.y, other.y),
|
||||||
|
z: d(self.z, other.z),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn energy(&self) -> i32 {
|
||||||
|
self.x.abs() + self.y.abs() + self.z.abs()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,3 +8,4 @@ pub mod day08;
|
|||||||
pub mod day09;
|
pub mod day09;
|
||||||
pub mod day10;
|
pub mod day10;
|
||||||
pub mod day11;
|
pub mod day11;
|
||||||
|
pub mod day12;
|
||||||
|
|||||||
Reference in New Issue
Block a user