summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Tuomi <jan.tuomi@eficode.com>2018-12-10 18:32:09 +0200
committerJan Tuomi <jan.tuomi@eficode.com>2018-12-10 18:32:09 +0200
commit49230f87d81c21f11c6d227109b7b9eaee52608f (patch)
treeb10589341472bcab4d428943878733d70bdebf4e
parentb8e9c88da10835a32c82891c93023eb6eda952ed (diff)
Solve day10
-rw-r--r--day10/.vscode/launch.json43
-rw-r--r--day10/Cargo.lock14
-rw-r--r--day10/Cargo.toml7
-rw-r--r--day10/input_ex.txt31
-rw-r--r--day10/src/main.rs121
5 files changed, 216 insertions, 0 deletions
diff --git a/day10/.vscode/launch.json b/day10/.vscode/launch.json
new file mode 100644
index 0000000..0e96ed8
--- /dev/null
+++ b/day10/.vscode/launch.json
@@ -0,0 +1,43 @@
+{
+ // Use IntelliSense to learn about possible attributes.
+ // Hover to view descriptions of existing attributes.
+ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
+ "version": "0.2.0",
+ "configurations": [
+ {
+ "type": "lldb",
+ "request": "launch",
+ "name": "Debug executable 'day10'",
+ "cargo": {
+ "args": [
+ "build",
+ "--bin=day10",
+ "--package=day10"
+ ],
+ "filter": {
+ "kind": "bin"
+ }
+ },
+ "args": ["input.txt"],
+ "cwd": "${workspaceFolder}"
+ },
+ {
+ "type": "lldb",
+ "request": "launch",
+ "name": "Debug unit tests in executable 'day10'",
+ "cargo": {
+ "args": [
+ "test",
+ "--no-run",
+ "--bin=day10",
+ "--package=day10"
+ ],
+ "filter": {
+ "kind": "bin"
+ }
+ },
+ "args": [],
+ "cwd": "${workspaceFolder}"
+ }
+ ]
+} \ No newline at end of file
diff --git a/day10/Cargo.lock b/day10/Cargo.lock
new file mode 100644
index 0000000..8312734
--- /dev/null
+++ b/day10/Cargo.lock
@@ -0,0 +1,14 @@
+[[package]]
+name = "day10"
+version = "0.1.0"
+dependencies = [
+ "text_io 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "text_io"
+version = "0.1.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[metadata]
+"checksum text_io 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "9658b61ebd1d2a40c276ba2335890b9eb6550b67458a6fbce2022e58c3350a50"
diff --git a/day10/Cargo.toml b/day10/Cargo.toml
new file mode 100644
index 0000000..c4a7df0
--- /dev/null
+++ b/day10/Cargo.toml
@@ -0,0 +1,7 @@
+[package]
+name = "day10"
+version = "0.1.0"
+authors = ["Jan Tuomi <jan.tuomi@eficode.com>"]
+
+[dependencies]
+text_io = "0.1.7"
diff --git a/day10/input_ex.txt b/day10/input_ex.txt
new file mode 100644
index 0000000..794273a
--- /dev/null
+++ b/day10/input_ex.txt
@@ -0,0 +1,31 @@
+position=< 9, 1> velocity=< 0, 2>
+position=< 7, 0> velocity=<-1, 0>
+position=< 3, -2> velocity=<-1, 1>
+position=< 6, 10> velocity=<-2, -1>
+position=< 2, -4> velocity=< 2, 2>
+position=<-6, 10> velocity=< 2, -2>
+position=< 1, 8> velocity=< 1, -1>
+position=< 1, 7> velocity=< 1, 0>
+position=<-3, 11> velocity=< 1, -2>
+position=< 7, 6> velocity=<-1, -1>
+position=<-2, 3> velocity=< 1, 0>
+position=<-4, 3> velocity=< 2, 0>
+position=<10, -3> velocity=<-1, 1>
+position=< 5, 11> velocity=< 1, -2>
+position=< 4, 7> velocity=< 0, -1>
+position=< 8, -2> velocity=< 0, 1>
+position=<15, 0> velocity=<-2, 0>
+position=< 1, 6> velocity=< 1, 0>
+position=< 8, 9> velocity=< 0, -1>
+position=< 3, 3> velocity=<-1, 1>
+position=< 0, 5> velocity=< 0, -1>
+position=<-2, 2> velocity=< 2, 0>
+position=< 5, -2> velocity=< 1, 2>
+position=< 1, 4> velocity=< 2, 1>
+position=<-2, 7> velocity=< 2, -2>
+position=< 3, 6> velocity=<-1, -1>
+position=< 5, 0> velocity=< 1, 0>
+position=<-6, 0> velocity=< 2, 0>
+position=< 5, 9> velocity=< 1, -2>
+position=<14, 7> velocity=<-2, 0>
+position=<-3, 6> velocity=< 2, -1> \ No newline at end of file
diff --git a/day10/src/main.rs b/day10/src/main.rs
new file mode 100644
index 0000000..8674c8f
--- /dev/null
+++ b/day10/src/main.rs
@@ -0,0 +1,121 @@
+use std::env;
+use std::io::BufReader;
+use std::io::BufRead;
+use std::fs::File;
+use std::process;
+use std::io::{stdin,stdout,Write};
+
+#[macro_use] extern crate text_io;
+
+struct Point {
+ x: i64,
+ y: i64,
+ vx: i64,
+ vy: i64
+}
+
+struct BoundingBox {
+ x: i64,
+ y: i64,
+ w: i64,
+ h: i64
+}
+
+static WIDTH: i64 = 200;
+static HEIGHT: i64 = 20;
+
+fn visualize(points: &Vec<Point>, bb: &BoundingBox) {
+ let mut grid: Vec<Vec<char>> = vec![vec!['.'; bb.w as usize]; bb.h as usize];
+ for point in points {
+ let (xi, yi) = transform(point, bb);
+ grid[yi][xi] = '#';
+ }
+
+ for row in &grid {
+ for elem in row {
+ print!("{}", elem);
+ }
+ print!("\n");
+ }
+}
+
+fn state(points: &Vec<Point>, time: i64) -> Vec<Point> {
+ points.iter().map(|p| Point {
+ x: p.x + time * p.vx,
+ y: p.y + time * p.vy,
+ vx: p.vx,
+ vy: p.vy
+ }).collect()
+}
+
+fn transform(point: &Point, bb: &BoundingBox) -> (usize, usize) {
+ let x = point.x - bb.x;
+ let y = point.y - bb.y;
+
+ (x as usize, y as usize)
+}
+
+fn calc_bounding_box(points: &Vec<Point>) -> BoundingBox {
+ let px_min = points.iter().min_by(|&a, &b| a.x.cmp(&b.x)).unwrap().x;
+ let px_max = points.iter().max_by(|&a, &b| a.x.cmp(&b.x)).unwrap().x;
+ let py_min = points.iter().min_by(|&a, &b| a.y.cmp(&b.y)).unwrap().y;
+ let py_max = points.iter().max_by(|&a, &b| a.y.cmp(&b.y)).unwrap().y;
+
+ let pw = px_max - px_min + 1;
+ let ph = py_max - py_min + 1;
+
+ let bb = BoundingBox {
+ x: px_min,
+ y: py_min,
+ w: pw,
+ h: ph
+ };
+
+ bb
+}
+
+fn main() {
+ let args: Vec<String> = env::args().collect();
+ if args.len() != 2 {
+ println!("Wrong number of arguments. Provide a file name and worker count.");
+ process::exit(1);
+ }
+ let filename = &args[1];
+
+ println!("Using file {} as input.", filename);
+ let f = match File::open(filename) {
+ Ok(file) => file,
+ Err(e) => {
+ println!("Failed to open file {}. {:?}", filename, e);
+ process::exit(1);
+ }
+ };
+ let file = BufReader::new(&f);
+ let lines: Vec<String> = file.lines()
+ .map(|line| line.expect("Could not parse line."))
+ .collect();
+
+ let mut init_points: Vec<Point> = Vec::new();
+ for line in &lines {
+ let ln = line
+ .replace("< ", "<")
+ .replace(" ", " ");
+ let (x, y): (i64, i64);
+ let (vx, vy): (i64, i64);
+ scan!(ln.bytes() => "position=<{}, {}> velocity=<{}, {}>", x, y, vx, vy);
+ init_points.push(Point { x, y, vx, vy });
+ }
+
+ let mut time = 0;
+ let step = 1;
+ // println!("Time: {} s, step: {} s", time, step);
+ while time < 20000 {
+ let points = state(&init_points, time);
+ let bb = calc_bounding_box(&points);
+ if bb.w <= WIDTH && bb.h <= HEIGHT {
+ println!("Time: {} s", time);
+ visualize(&points, &bb);
+ }
+ time += step;
+ }
+} \ No newline at end of file