From 49230f87d81c21f11c6d227109b7b9eaee52608f Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Mon, 10 Dec 2018 18:32:09 +0200 Subject: Solve day10 --- day10/.vscode/launch.json | 43 ++++++++++++++++ day10/Cargo.lock | 14 ++++++ day10/Cargo.toml | 7 +++ day10/input_ex.txt | 31 ++++++++++++ day10/src/main.rs | 121 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 216 insertions(+) create mode 100644 day10/.vscode/launch.json create mode 100644 day10/Cargo.lock create mode 100644 day10/Cargo.toml create mode 100644 day10/input_ex.txt create mode 100644 day10/src/main.rs (limited to 'day10') 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 "] + +[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, bb: &BoundingBox) { + let mut grid: Vec> = 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, time: i64) -> Vec { + 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) -> 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 = 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 = file.lines() + .map(|line| line.expect("Could not parse line.")) + .collect(); + + let mut init_points: Vec = 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 -- cgit v1.3