diff --git a/day12/Cargo.toml b/day12/Cargo.toml new file mode 100644 index 0000000..87a946c --- /dev/null +++ b/day12/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "day12" +version = "0.1.0" +authors = ["henine "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day12/input b/day12/input new file mode 100644 index 0000000..81af1c4 --- /dev/null +++ b/day12/input @@ -0,0 +1,779 @@ +E1 +S4 +W5 +N5 +W2 +L90 +E5 +R90 +F73 +E1 +N3 +E5 +R180 +E1 +F26 +N5 +W5 +R180 +E5 +F50 +E4 +F63 +S2 +F24 +R90 +E2 +L90 +N2 +F94 +S4 +F70 +N3 +F60 +L90 +W5 +R90 +E2 +R90 +F38 +R180 +S3 +L90 +R90 +S1 +L180 +W3 +F23 +N4 +R90 +N1 +E3 +S3 +F26 +S5 +E1 +E2 +S3 +E5 +N2 +E2 +L90 +F9 +S4 +F77 +F70 +F92 +N1 +W4 +R90 +S3 +F56 +N2 +F21 +R90 +E5 +W5 +F87 +E1 +L90 +N3 +W4 +N4 +F86 +L90 +W2 +E2 +F94 +S1 +E5 +L90 +W5 +N4 +W4 +S2 +W4 +L180 +F29 +R180 +E2 +F12 +S3 +W4 +F48 +L90 +W4 +L90 +F69 +N1 +F51 +L270 +F62 +W2 +R90 +W2 +F5 +L90 +F62 +E3 +N3 +W5 +R90 +E3 +L90 +F6 +E1 +W5 +F72 +R180 +E5 +L180 +F38 +E1 +N5 +R90 +F98 +N4 +F69 +W3 +R270 +N4 +F53 +E3 +N5 +R180 +F2 +R90 +L90 +F38 +N3 +E1 +F100 +E3 +L90 +N5 +F74 +F38 +W2 +F8 +N4 +E3 +F84 +L270 +S1 +F38 +S3 +F92 +L90 +F12 +S3 +F83 +E5 +R90 +E1 +N3 +F70 +N4 +F47 +N5 +L90 +F29 +R90 +S2 +W5 +F20 +R90 +E3 +F58 +N2 +E1 +F2 +R90 +E5 +F9 +E3 +L180 +E4 +F11 +E5 +L90 +F64 +W1 +F13 +R90 +W4 +R90 +N1 +E3 +F29 +S5 +W3 +N3 +W3 +L90 +F100 +E2 +F93 +R90 +F34 +N1 +F51 +R90 +E2 +R90 +W4 +L90 +E3 +R180 +W1 +W4 +S2 +F88 +S3 +W3 +N4 +E5 +S4 +E2 +F80 +R90 +S5 +R90 +F46 +E1 +N2 +L90 +F52 +F92 +W4 +R90 +S5 +F21 +N2 +F37 +S1 +L180 +N4 +E1 +L90 +F17 +L90 +F95 +E5 +R180 +W2 +L90 +W4 +R180 +S2 +W1 +E2 +N2 +F88 +E3 +R180 +W4 +L180 +E2 +S2 +R90 +S5 +L90 +S1 +L90 +N4 +W4 +F52 +S5 +F23 +W2 +R90 +F100 +W1 +N3 +L90 +E1 +F8 +L90 +F97 +N1 +F46 +N3 +F10 +W3 +R180 +N5 +L90 +F85 +N5 +L90 +E1 +F29 +L90 +E3 +W3 +S5 +L270 +W1 +N3 +E3 +F87 +R180 +F15 +S1 +R180 +N1 +F37 +E3 +R90 +E1 +N3 +L90 +F73 +R90 +F15 +F6 +L180 +S5 +F8 +S2 +R90 +W3 +N1 +L180 +F87 +N4 +R180 +N5 +L90 +W3 +N5 +R90 +E4 +F35 +E1 +L90 +S1 +E1 +F58 +E1 +W3 +S1 +L270 +F65 +W3 +F27 +S2 +F68 +R90 +E5 +F80 +R90 +W5 +R180 +S5 +L90 +W1 +F63 +E2 +R270 +S2 +R90 +L90 +E4 +R90 +S3 +R270 +L180 +F78 +W4 +R90 +W1 +F33 +N1 +F1 +F79 +R90 +W5 +S5 +N3 +L270 +E1 +R180 +S1 +N1 +W2 +R90 +S4 +R270 +F49 +F39 +E2 +F6 +N5 +R90 +N2 +E3 +L90 +F30 +L180 +W2 +L90 +F39 +L270 +S3 +F89 +N2 +R90 +S5 +F95 +W2 +L90 +S2 +L180 +S3 +F74 +L90 +S2 +W1 +S3 +E1 +R90 +S5 +L180 +N3 +L90 +F52 +W1 +L90 +F79 +N4 +F19 +R180 +E3 +R270 +F52 +L180 +E5 +R270 +E5 +N3 +W4 +F57 +S5 +R180 +N2 +L180 +W4 +L90 +W4 +L90 +F98 +S4 +E3 +F22 +N3 +W5 +F9 +E3 +F25 +R180 +F76 +N1 +F39 +R90 +F81 +N5 +F73 +W2 +F80 +N3 +R90 +W3 +F63 +R270 +E1 +R90 +W2 +L90 +S5 +R90 +F5 +N1 +F98 +F20 +E4 +L90 +F69 +S3 +L180 +L90 +F99 +L90 +F52 +L90 +F29 +E5 +N4 +R180 +R90 +E2 +R90 +S5 +E3 +L270 +F77 +E2 +F8 +W1 +N4 +F38 +S4 +W4 +S3 +L90 +E3 +N4 +R90 +F99 +S3 +E2 +F80 +N5 +F43 +N3 +F99 +L90 +S4 +F66 +L90 +L90 +N2 +F80 +N3 +F86 +S4 +R270 +S3 +E4 +F83 +E2 +R90 +F26 +E3 +N5 +E2 +S4 +F60 +E5 +S3 +R90 +W4 +S5 +E2 +R90 +F67 +N1 +E3 +N2 +L90 +E4 +L90 +W5 +E1 +R90 +S2 +W1 +N1 +F39 +F23 +F64 +R180 +F82 +S4 +W1 +F18 +F57 +W5 +F55 +L90 +N5 +L90 +W2 +L90 +F12 +E4 +R90 +W4 +F37 +W3 +F72 +R90 +N5 +R90 +F95 +W3 +L180 +F6 +W5 +F67 +R90 +F60 +R90 +F77 +E4 +R180 +F81 +E1 +F39 +E2 +N2 +F95 +N5 +F69 +L90 +N3 +R90 +S1 +F11 +N1 +L180 +E2 +F47 +W2 +L90 +N2 +W3 +N2 +R180 +F20 +L90 +F17 +S3 +E4 +L270 +W4 +S2 +L180 +W1 +S1 +R180 +N1 +W4 +N3 +L180 +N4 +F30 +E5 +R90 +S2 +F62 +L90 +E5 +F17 +L90 +W2 +R180 +E5 +S2 +F8 +R180 +S5 +F56 +S1 +E3 +F91 +L90 +E1 +R90 +S3 +R90 +E3 +R90 +S1 +E2 +F56 +E5 +L90 +S4 +F44 +S2 +L90 +F38 +E3 +S5 +E5 +S5 +W3 +N1 +R90 +N5 +F82 +W2 +S3 +R90 +E4 +R90 +E5 +E1 +F64 +N2 +L180 +S1 +E2 +N4 +L90 +F7 +R180 +S3 +F79 +W4 +S2 +L180 +E5 +R180 +N1 +L90 +E2 +R90 +F92 +S4 +F42 +E4 +R180 +W3 +R90 +N3 +F9 +L90 +W5 +F4 +R90 +S4 +F39 +N3 +F78 +E5 +N2 +F79 +S3 +R90 +N2 +F2 +S1 +F94 +N2 +W2 +F17 +R90 +W1 +S3 +E2 +F83 +L180 +W2 +N3 +R270 +N5 +W4 +F40 diff --git a/day12/src/main.rs b/day12/src/main.rs new file mode 100644 index 0000000..33088b0 --- /dev/null +++ b/day12/src/main.rs @@ -0,0 +1,135 @@ +use std::fs::File; +use std::io::{BufRead, BufReader}; +// use crate::Direction::{North, East, West, South}; +use crate::Command::{N, R, L, W, S, E, F}; + +// #[derive(Copy, Clone, Debug)] +// enum Direction { +// East(i32), +// North(i32), +// West(i32), +// South(i32), +// } + +// impl Direction { +// fn turn(&self, degrees: i32) -> Direction { +// match self { +// East(d) => Direction::new((d + degrees + 360) % 360), +// North(d) => Direction::new((d + degrees + 360) % 360), +// West(d) => Direction::new((d + degrees + 360) % 360), +// South(d) => Direction::new((d + degrees + 360) % 360), +// } +// } +// +// fn new(degrees: i32) -> Direction { +// match degrees { +// 0 => East(0), +// 90 => North(90), +// 180 => West(180), +// 270 => South(270), +// n => panic!(n) +// } +// } +// } + +#[derive(Copy, Clone, Debug)] +struct Ship { + // direction: Direction, + v: Velocity, + x: i32, + y: i32, +} + +impl Ship { + fn new() -> Self { + Self { + // direction: Direction::new(0), + v: Velocity::new(), + x: 0, + y: 0, + } + } +} + +#[derive(Copy, Clone, Debug)] +struct Velocity { + x: i32, + y: i32, +} + +impl Velocity { + fn new() -> Self { + Velocity { x: 10, y: 1 } + } + + fn rotate(self, angle: i32) -> Velocity { + match angle { + 0 => self, + 90 => Velocity { x: -self.y, y: self.x }, + 180 => Velocity { x: -self.x, y: -self.y }, + 270 => Velocity { x: self.y, y: -self.x }, + // -0 => self, + -90 => Velocity { x: self.y, y: -self.x }, + -180 => Velocity { x: -self.x, y: -self.y }, + -270 => Velocity { x: -self.y, y: self.x }, + n => panic!(n) + } + } +} + +enum Command { + N(i32), + S(i32), + E(i32), + W(i32), + L(i32), + R(i32), + F(i32), +} + +impl Command { + fn parse(command: String) -> Self { + match &command[0..1] { + "N" => N(command[1..command.len()].parse::().unwrap()), + "S" => S(command[1..command.len()].parse::().unwrap()), + "E" => E(command[1..command.len()].parse::().unwrap()), + "W" => W(command[1..command.len()].parse::().unwrap()), + "L" => L(command[1..command.len()].parse::().unwrap()), + "R" => R(command[1..command.len()].parse::().unwrap()), + "F" => F(command[1..command.len()].parse::().unwrap()), + e => panic!(e.to_string()) + } + } + + fn execute(&self, ship: Ship) -> Ship { + match self { + N(dist) => Ship { v: Velocity { x: ship.v.x, y: ship.v.y + dist }, x: ship.x, y: ship.y }, + S(dist) => Ship { v: Velocity { x: ship.v.x, y: ship.v.y - dist }, x: ship.x, y: ship.y }, + E(dist) => Ship { v: Velocity { x: ship.v.x + dist, y: ship.v.y }, x: ship.x, y: ship.y }, + W(dist) => Ship { v: Velocity { x: ship.v.x - dist, y: ship.v.y }, x: ship.x, y: ship.y }, + L(angle) => Ship { v: ship.v.rotate(*angle), x: ship.x, y: ship.y }, + R(angle) => Ship { v: ship.v.rotate(-*angle), x: ship.x, y: ship.y }, + F(dist) => Ship { v: ship.v, x: ship.x + dist * ship.v.x, y: ship.y + dist * ship.v.y } + } + } +} + +fn main() { + let file = File::open("input").unwrap(); + + let input_buffer = BufReader::new(&file); + let lines = input_buffer.lines(); + + let final_position: Ship = lines + .map(Result::unwrap) + .map(|x: String| x.trim().to_string()) + .map(Command::parse) + .fold(Ship::new(), |ship, command| { + println!("{:?}", ship); + command.execute(ship) + }); + println!("{:?}", final_position); + + + println!("{}", final_position.x.abs() + final_position.y.abs()) +} diff --git a/day12/test b/day12/test new file mode 100644 index 0000000..48c2a50 --- /dev/null +++ b/day12/test @@ -0,0 +1,5 @@ +F10 +N3 +F7 +R90 +F11 \ No newline at end of file