summary refs log tree commit diff
diff options
context:
space:
mode:
authorIrene Knapp <ireneista@gmail.com>2020-12-11 21:51:42 -0800
committerIrene Knapp <ireneista@gmail.com>2020-12-11 21:51:42 -0800
commitfbc6c484188873b0063dfb16a99f10d1c1d125b6 (patch)
tree6f5d14a060c2138ed87347e2ec7242eebd3bc788
parentee6e752de4be6bbe1e1fc4713504be068373a140 (diff)
12
-rw-r--r--12/Cargo.toml11
-rw-r--r--12/input774
-rw-r--r--12/input.small5
-rw-r--r--12/src/main.rs144
-rw-r--r--12/tests/main.rs14
-rw-r--r--Cargo.lock8
-rw-r--r--Cargo.nix35
-rw-r--r--Cargo.toml1
8 files changed, 992 insertions, 0 deletions
diff --git a/12/Cargo.toml b/12/Cargo.toml
new file mode 100644
index 0000000..441770e
--- /dev/null
+++ b/12/Cargo.toml
@@ -0,0 +1,11 @@
+[package]
+name = "advent_12"
+version = "0.1.0"
+authors = ["Irene Knapp <ireneista@gmail.com>"]
+edition = "2018"
+
+[dependencies]
+advent_lib = { path = "../lib" }
+
+[dev-dependencies]
+assert_cmd = "0.10"
diff --git a/12/input b/12/input
new file mode 100644
index 0000000..7b923fc
--- /dev/null
+++ b/12/input
@@ -0,0 +1,774 @@
+E2
+L180
+S4
+R90
+S1
+F49
+N2
+F18
+N2
+L180
+S5
+L90
+E3
+N2
+F11
+L180
+N5
+W3
+L180
+W2
+N5
+F80
+R90
+F89
+N1
+L180
+N2
+R180
+E4
+R90
+S1
+L90
+N5
+R180
+N2
+F17
+L90
+E2
+F58
+W5
+L90
+W3
+N3
+F78
+L90
+N4
+L90
+F15
+W1
+R90
+S1
+W4
+R90
+F41
+W4
+S4
+F37
+E5
+S1
+E3
+F19
+R90
+S1
+W4
+S2
+E2
+L180
+F51
+W5
+R90
+F76
+E2
+F40
+N4
+R180
+E5
+N3
+F72
+S4
+R90
+F99
+E3
+F76
+W5
+R90
+E2
+S5
+R180
+F76
+N4
+L180
+F10
+F83
+S1
+F46
+L90
+S5
+E1
+S1
+F14
+N4
+E1
+R180
+E1
+R180
+S3
+F52
+L90
+S4
+L90
+W3
+F18
+S2
+F81
+L180
+F76
+L180
+W1
+S2
+F73
+N2
+F77
+W1
+F28
+L180
+N2
+F76
+L180
+W5
+F61
+N4
+E2
+R180
+S2
+L90
+F14
+R180
+N5
+E4
+F11
+E1
+L90
+N3
+E3
+F58
+W3
+F72
+W1
+N2
+W5
+F44
+L90
+W4
+F37
+L90
+F4
+W5
+N3
+F57
+N4
+L90
+S2
+F43
+S4
+W3
+S5
+F84
+S2
+L90
+N1
+R180
+W3
+L180
+N2
+W3
+R90
+N2
+R90
+F66
+L90
+F73
+E4
+S2
+L270
+W2
+E2
+S4
+E1
+R90
+W1
+F49
+L270
+F70
+S3
+W1
+N2
+E1
+F65
+W3
+R90
+F27
+E5
+F80
+S4
+W5
+F68
+E5
+R90
+F94
+W2
+L90
+F37
+L180
+E1
+F38
+N2
+F15
+N4
+E3
+L90
+E1
+R90
+S1
+E5
+N4
+N2
+E4
+L270
+N4
+R90
+E1
+S4
+E4
+F71
+E2
+R180
+N5
+E3
+F17
+L90
+N2
+W1
+L90
+F62
+W1
+F85
+W1
+L180
+F33
+S4
+W1
+N5
+F81
+W2
+R90
+S2
+F49
+L180
+S5
+F4
+W4
+N3
+W1
+F17
+R90
+W4
+N4
+E4
+N3
+L90
+S2
+R90
+S3
+L90
+E4
+R90
+S5
+F88
+S1
+L180
+N5
+E5
+F55
+R90
+F81
+E5
+L180
+R90
+F55
+R90
+W5
+F13
+R90
+N5
+F58
+L180
+S5
+F27
+E4
+S3
+F42
+R90
+F39
+W3
+S3
+F31
+S4
+L90
+W1
+S3
+W3
+N4
+W2
+S3
+L90
+F61
+E1
+F23
+S2
+F31
+S3
+L180
+W1
+N1
+L90
+N3
+F81
+E2
+N1
+R90
+F64
+S4
+F88
+E1
+N5
+W1
+S3
+F10
+N5
+L90
+F58
+S1
+R90
+E3
+L90
+N4
+F94
+S1
+W1
+S4
+L90
+F51
+L180
+N4
+R90
+L90
+N4
+F66
+W2
+S3
+S3
+W4
+F68
+L90
+F42
+E1
+F43
+R90
+N3
+F20
+E1
+N3
+E4
+N3
+F4
+S4
+R180
+W1
+R270
+N3
+F86
+L90
+E5
+F84
+N3
+W3
+F16
+L90
+N2
+E3
+L90
+S5
+E5
+F53
+L270
+N2
+F91
+R90
+E5
+N4
+F57
+E5
+S5
+F61
+S4
+F89
+E3
+N3
+N5
+F3
+S5
+F59
+E5
+F66
+R180
+S1
+W1
+N2
+R180
+S4
+E2
+L90
+N1
+W2
+F13
+L90
+E5
+F6
+W3
+F78
+E1
+F7
+W1
+N4
+W5
+F58
+R90
+E4
+N3
+E5
+N3
+W1
+S3
+R90
+F16
+L90
+F93
+R270
+N5
+F2
+W1
+S3
+F54
+R270
+F18
+R180
+F95
+L90
+W1
+E4
+N2
+W1
+L90
+S2
+L90
+W2
+S4
+F92
+W2
+S3
+R180
+N5
+E3
+N5
+E5
+F22
+F88
+S3
+E2
+R90
+S5
+W1
+L90
+E4
+F77
+N1
+W3
+F14
+E3
+R90
+W1
+F21
+N1
+F58
+W4
+N2
+R90
+N2
+W4
+F68
+W5
+N3
+L90
+F22
+R90
+F90
+F84
+S5
+F30
+N1
+W4
+R90
+F17
+R90
+W4
+S5
+E2
+N1
+F92
+N2
+R180
+N5
+E2
+R90
+F38
+R90
+F15
+E5
+N4
+N4
+E4
+S4
+F92
+R90
+F22
+S3
+W4
+N3
+E1
+R180
+F96
+L90
+E1
+N1
+F9
+W2
+N4
+F17
+N2
+R90
+F76
+S2
+F5
+S5
+F34
+R90
+F7
+N4
+F83
+N5
+L90
+W1
+S3
+R90
+S3
+W2
+S3
+F51
+N5
+W4
+F8
+E3
+F10
+N5
+F39
+S3
+E2
+L90
+E5
+L90
+N5
+E2
+N3
+F42
+S3
+F38
+N5
+F19
+F97
+W2
+R180
+S4
+E4
+S2
+W3
+F39
+W4
+F70
+S1
+W1
+R90
+F41
+L90
+E1
+N1
+E3
+W5
+F13
+E4
+F2
+R180
+F27
+E4
+N2
+L270
+E1
+N3
+W4
+F81
+W3
+R90
+E1
+F57
+S5
+R90
+F13
+L180
+N5
+F98
+F32
+N3
+R90
+N3
+W3
+S3
+W3
+N4
+F73
+L180
+N1
+E4
+F7
+E4
+R90
+N4
+R90
+S2
+E5
+F32
+S2
+N5
+W3
+R90
+W5
+S2
+L90
+F4
+R270
+N5
+E3
+L90
+S5
+F24
+N4
+R90
+F27
+L90
+F16
+R90
+N2
+R90
+N3
+L90
+S3
+L90
+F85
+S3
+F47
+N1
+E1
+N3
+R270
+S2
+L90
+F50
+L90
+S2
+F23
+N4
+L180
+E3
+F91
+R90
+E1
+W4
+F81
+W5
+R90
+F46
+E1
+W1
+F91
+N5
+W5
+N3
+W1
+L90
+F60
+S2
+L90
+E1
+F82
+S3
+W5
+N5
+F90
+E3
+S1
+F61
+E4
+F98
+R180
+F8
+R270
+F73
+W4
+L90
+W5
+L90
+F86
+W5
+L180
+F61
+N5
+F88
+E2
+L270
+F90
+N5
+F21
+R270
+F40
+L90
+W1
+N2
+L90
+E2
+S5
+E2
+S1
+E5
+N3
+F51
+S1
+F58
+W3
+L180
+F13
+R90
+N1
+F79
+W2
+F61
+R90
+F22
+E2
+N5
+F1
+S4
+F99
+S1
+S3
+E2
+F97
diff --git a/12/input.small b/12/input.small
new file mode 100644
index 0000000..d382291
--- /dev/null
+++ b/12/input.small
@@ -0,0 +1,5 @@
+F10
+N3
+F7
+R90
+F11
diff --git a/12/src/main.rs b/12/src/main.rs
new file mode 100644
index 0000000..33999a9
--- /dev/null
+++ b/12/src/main.rs
@@ -0,0 +1,144 @@
+use advent_lib::prelude::*;
+
+//use std::convert::TryFrom;
+
+#[derive(Debug)]
+struct ShipState {
+  x_offset: i64, // positive is east
+  y_offset: i64, // positive is north
+  heading: i64, // 0 is east; 90 is north
+}
+
+#[derive(Debug)]
+struct NavState {
+  waypoint_x_offset: i64,
+  waypoint_y_offset: i64,
+  ship_x_offset: i64,
+  ship_y_offset: i64,
+}
+
+
+impl ShipState {
+  fn new() -> ShipState {
+    ShipState {
+      x_offset: 0,
+      y_offset: 0,
+      heading: 0,
+    }
+  }
+}
+
+impl NavState {
+  fn new() -> NavState {
+    NavState {
+      waypoint_x_offset: 10,
+      waypoint_y_offset: 1,
+      ship_x_offset: 0,
+      ship_y_offset: 0,
+    }
+  }
+}
+
+
+fn main() -> Result<()> {
+  let mut args = std::env::args();
+  if args.len() != 2 {
+    eprintln!("Usage: advent input");
+  }
+  let _ = args.next();
+  let filename = args.next().unwrap();
+
+  let input = advent_lib::read_lines_file(&filename)?;
+
+  let mut ship = ShipState::new();
+  let mut nav = NavState::new();
+
+  for line in &input {
+    let (command, parameter_str) = line.split_at(1);
+    let parameter = parameter_str.parse::<i64>()?;
+
+    match command {
+      "N" => {
+        ship.y_offset += parameter;
+        nav.waypoint_y_offset += parameter;
+      },
+      "S" => {
+        ship.y_offset -= parameter;
+        nav.waypoint_y_offset -= parameter;
+      },
+      "E" => {
+        ship.x_offset += parameter;
+        nav.waypoint_x_offset += parameter;
+      },
+      "W" => {
+        ship.x_offset -= parameter;
+        nav.waypoint_x_offset -= parameter;
+      },
+      "L" => {
+        ship.heading = (ship.heading + parameter) % 360;
+
+        rotate_waypoint(&mut nav, parameter);
+      },
+      "R" => {
+        ship.heading = (ship.heading - parameter + 360) % 360;
+
+        rotate_waypoint(&mut nav, -parameter);
+      },
+      "F" => {
+        while ship.heading < 0 {
+          ship.heading += 360;
+        }
+        ship.heading = ship.heading % 360;
+
+        if ship.heading == 0 {
+          ship.x_offset += parameter;
+        } else if ship.heading == 90 {
+          ship.y_offset += parameter;
+        } else if ship.heading == 180 {
+          ship.x_offset -= parameter;
+        } else if ship.heading == 270 {
+          ship.y_offset -= parameter;
+        }
+
+        nav.ship_x_offset += parameter * nav.waypoint_x_offset;
+        nav.ship_y_offset += parameter * nav.waypoint_y_offset;
+      },
+      _ => { },
+    }
+  }
+
+  let ship_distance = ship.x_offset.abs() + ship.y_offset.abs();
+  let nav_distance = nav.ship_x_offset.abs() + nav.ship_y_offset.abs();
+  println!("{}", ship_distance);
+  println!("{}", nav_distance);
+
+  Ok(())
+}
+
+
+fn rotate_waypoint(nav: &mut NavState, parameter: i64) {
+  let mut rotation = parameter;
+  while rotation < 0 {
+    rotation += 360;
+  }
+  rotation = rotation % 360;
+
+  if rotation == 90 {
+    // counterclockwise
+    let x_tmp = nav.waypoint_x_offset;
+    let y_tmp = nav.waypoint_y_offset;
+    nav.waypoint_y_offset = x_tmp;
+    nav.waypoint_x_offset = -y_tmp;
+  } else if rotation == 270 {
+    // clockwise
+    let x_tmp = nav.waypoint_x_offset;
+    let y_tmp = nav.waypoint_y_offset;
+    nav.waypoint_y_offset = -x_tmp;
+    nav.waypoint_x_offset = y_tmp;
+  } else if rotation == 180 {
+    let x_tmp = nav.waypoint_x_offset;
+    let y_tmp = nav.waypoint_y_offset;
+    nav.waypoint_x_offset = -x_tmp;
+    nav.waypoint_y_offset = -y_tmp;
+  }
+}
diff --git a/12/tests/main.rs b/12/tests/main.rs
new file mode 100644
index 0000000..e883e41
--- /dev/null
+++ b/12/tests/main.rs
@@ -0,0 +1,14 @@
+use assert_cmd::prelude::*;
+use std::process::Command;
+
+
+#[test]
+fn personal_input() -> Result<(), Box<dyn std::error::Error>> {
+  let mut command = Command::cargo_bin("advent_12")?;
+
+  command.arg("input");
+  command.assert().success().stdout("858\n39140\n");
+
+  Ok(())
+}
+
diff --git a/Cargo.lock b/Cargo.lock
index 1c31fa8..5840f89 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -91,6 +91,14 @@ dependencies = [
 ]
 
 [[package]]
+name = "advent_12"
+version = "0.1.0"
+dependencies = [
+ "advent_lib",
+ "assert_cmd",
+]
+
+[[package]]
 name = "advent_lib"
 version = "0.1.0"
 
diff --git a/Cargo.nix b/Cargo.nix
index 785f38c..1979e31 100644
--- a/Cargo.nix
+++ b/Cargo.nix
@@ -138,6 +138,16 @@ rec {
       # File a bug if you depend on any for non-debug work!
       debug = internal.debugCrate { inherit packageId; };
     };
+    "advent_12" = rec {
+      packageId = "advent_12";
+      build = internal.buildRustCrateWithFeatures {
+        packageId = "advent_12";
+      };
+
+      # Debug support which might change between releases.
+      # File a bug if you depend on any for non-debug work!
+      debug = internal.debugCrate { inherit packageId; };
+    };
     "advent_lib" = rec {
       packageId = "advent_lib";
       build = internal.buildRustCrateWithFeatures {
@@ -453,6 +463,31 @@ rec {
         ];
         
       };
+      "advent_12" = rec {
+        crateName = "advent_12";
+        version = "0.1.0";
+        edition = "2018";
+        crateBin = [
+          { name = "advent_12"; path = "src/main.rs"; }
+        ];
+        src = (builtins.filterSource sourceFilter ./12);
+        authors = [
+          "Irene Knapp <ireneista@gmail.com>"
+        ];
+        dependencies = [
+          {
+            name = "advent_lib";
+            packageId = "advent_lib";
+          }
+        ];
+        devDependencies = [
+          {
+            name = "assert_cmd";
+            packageId = "assert_cmd";
+          }
+        ];
+        
+      };
       "advent_lib" = rec {
         crateName = "advent_lib";
         version = "0.1.0";
diff --git a/Cargo.toml b/Cargo.toml
index abc26f4..4eac77e 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -12,4 +12,5 @@ members = [
   "09",
   "10",
   "11",
+  "12",
 ]