mirror of
https://github.com/wezm/advent-of-code.git
synced 2024-11-08 17:22:31 +00:00
Add day 20 input parsing code
This commit is contained in:
parent
8f022e1d02
commit
61f1b3176e
5 changed files with 1193 additions and 0 deletions
88
2017/day/20/Cargo.lock
generated
Normal file
88
2017/day/20/Cargo.lock
generated
Normal file
|
@ -0,0 +1,88 @@
|
|||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "0.6.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "day20"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"regex 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.34"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"libc 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "0.2.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"aho-corasick 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"regex-syntax 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"thread_local 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "thread_local"
|
||||
version = "0.3.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unreachable"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "utf8-ranges"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "void"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[metadata]
|
||||
"checksum aho-corasick 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "d6531d44de723825aa81398a6415283229725a00fa30713812ab9323faa82fc4"
|
||||
"checksum lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c8f31047daa365f19be14b47c29df4f7c3b581832407daabe6ae77397619237d"
|
||||
"checksum libc 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)" = "36fbc8a8929c632868295d0178dd8f63fc423fd7537ad0738372bd010b3ac9b0"
|
||||
"checksum memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "796fba70e76612589ed2ce7f45282f5af869e0fdd7cc6199fa1aa1f1d591ba9d"
|
||||
"checksum regex 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ac6ab4e9218ade5b423358bbd2567d1617418403c7a512603630181813316322"
|
||||
"checksum regex-syntax 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ad890a5eef7953f55427c50575c680c42841653abd2b028b68cd223d157f62db"
|
||||
"checksum thread_local 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "279ef31c19ededf577bfd12dfae728040a21f635b06a24cd670ff510edd38963"
|
||||
"checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56"
|
||||
"checksum utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "662fab6525a98beff2921d7f61a39e7d59e0b425ebc7d0d9e66d316e55124122"
|
||||
"checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
|
7
2017/day/20/Cargo.toml
Normal file
7
2017/day/20/Cargo.toml
Normal file
|
@ -0,0 +1,7 @@
|
|||
[package]
|
||||
name = "day20"
|
||||
version = "0.1.0"
|
||||
authors = ["Wesley Moore <wes@wezm.net>"]
|
||||
|
||||
[dependencies]
|
||||
regex = "*"
|
1000
2017/day/20/input
Normal file
1000
2017/day/20/input
Normal file
File diff suppressed because it is too large
Load diff
35
2017/day/20/problem.txt
Normal file
35
2017/day/20/problem.txt
Normal file
|
@ -0,0 +1,35 @@
|
|||
--- Day 20: Particle Swarm ---
|
||||
|
||||
Suddenly, the GPU contacts you, asking for help. Someone has asked it to simulate too many particles, and it won't be able to finish them all in time to render the next frame at this rate.
|
||||
|
||||
It transmits to you a buffer (your puzzle input) listing each particle in order (starting with particle 0, then particle 1, particle 2, and so on). For each particle, it provides the X, Y, and Z coordinates for the particle's position (p), velocity (v), and acceleration (a), each in the format <X,Y,Z>.
|
||||
|
||||
Each tick, all particles are updated simultaneously. A particle's properties are updated in the following order:
|
||||
|
||||
Increase the X velocity by the X acceleration.
|
||||
Increase the Y velocity by the Y acceleration.
|
||||
Increase the Z velocity by the Z acceleration.
|
||||
Increase the X position by the X velocity.
|
||||
Increase the Y position by the Y velocity.
|
||||
Increase the Z position by the Z velocity.
|
||||
|
||||
Because of seemingly tenuous rationale involving z-buffering, the GPU would like to know which particle will stay closest to position <0,0,0> in the long term. Measure this using the Manhattan distance, which in this situation is simply the sum of the absolute values of a particle's X, Y, and Z position.
|
||||
|
||||
For example, suppose you are only given two particles, both of which stay entirely on the X-axis (for simplicity). Drawing the current states of particles 0 and 1 (in that order) with an adjacent a number line and diagram of current X positions (marked in parenthesis), the following would take place:
|
||||
|
||||
p=< 3,0,0>, v=< 2,0,0>, a=<-1,0,0> -4 -3 -2 -1 0 1 2 3 4
|
||||
p=< 4,0,0>, v=< 0,0,0>, a=<-2,0,0> (0)(1)
|
||||
|
||||
p=< 4,0,0>, v=< 1,0,0>, a=<-1,0,0> -4 -3 -2 -1 0 1 2 3 4
|
||||
p=< 2,0,0>, v=<-2,0,0>, a=<-2,0,0> (1) (0)
|
||||
|
||||
p=< 4,0,0>, v=< 0,0,0>, a=<-1,0,0> -4 -3 -2 -1 0 1 2 3 4
|
||||
p=<-2,0,0>, v=<-4,0,0>, a=<-2,0,0> (1) (0)
|
||||
|
||||
p=< 3,0,0>, v=<-1,0,0>, a=<-1,0,0> -4 -3 -2 -1 0 1 2 3 4
|
||||
p=<-8,0,0>, v=<-6,0,0>, a=<-2,0,0> (0)
|
||||
|
||||
At this point, particle 1 will never be closer to <0,0,0> than particle 0, and so, in the long run, particle 0 will stay closest.
|
||||
|
||||
Which particle will stay closest to position <0,0,0> in the long term?
|
||||
|
63
2017/day/20/src/main.rs
Normal file
63
2017/day/20/src/main.rs
Normal file
|
@ -0,0 +1,63 @@
|
|||
extern crate regex;
|
||||
|
||||
use regex::Regex;
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
use std::str::FromStr;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Point {
|
||||
x: isize,
|
||||
y: isize,
|
||||
z: isize
|
||||
}
|
||||
|
||||
impl Point {
|
||||
fn new(x: &str, y: &str, z: &str) -> Point {
|
||||
Point {
|
||||
x: isize::from_str(x).unwrap(),
|
||||
y: isize::from_str(y).unwrap(),
|
||||
z: isize::from_str(z).unwrap(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Particle {
|
||||
position: Point,
|
||||
velocity: Point,
|
||||
acceleration: Point,
|
||||
}
|
||||
|
||||
impl Particle {
|
||||
fn new(px: &str, py: &str, pz: &str, vx: &str, vy: &str, vz: &str, ax: &str, ay: &str, az: &str) -> Particle {
|
||||
Particle {
|
||||
position: Point::new(px, py, pz),
|
||||
velocity: Point::new(vx, vy, vz),
|
||||
acceleration: Point::new(ax, ay, az),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn main() {
|
||||
|
||||
// p=<-317,1413,1507>, v=<19,-102,-108>, a=<1,-3,-3>
|
||||
let re = Regex::new(r"p=<(-?\d+),(-?\d+),(-?\d+)>, v=<(-?\d+),(-?\d+),(-?\d+)>, a=<(-?\d+),(-?\d+),(-?\d+)>").unwrap();
|
||||
|
||||
let mut input = String::new();
|
||||
let mut file = File::open("input").expect("unable to open input file");
|
||||
file.read_to_string(&mut input).expect("error reading input");
|
||||
|
||||
let mut particles = Vec::new();
|
||||
for cap in re.captures_iter(&input) {
|
||||
let particle = Particle::new(&cap[1], &cap[2], &cap[3], &cap[4], &cap[5], &cap[6], &cap[7], &cap[8], &cap[9]);
|
||||
println!("{:?}", particle);
|
||||
particles.push(particle);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_example() {
|
||||
|
||||
}
|
Loading…
Reference in a new issue