summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--13/Cargo.toml11
-rw-r--r--13/input2
-rw-r--r--13/input.small2
-rw-r--r--13/src/main.rs82
-rw-r--r--13/tests/main.rs14
-rw-r--r--Cargo.lock8
-rw-r--r--Cargo.toml1
7 files changed, 120 insertions, 0 deletions
diff --git a/13/Cargo.toml b/13/Cargo.toml
new file mode 100644
index 0000000..f69c5e1
--- /dev/null
+++ b/13/Cargo.toml
@@ -0,0 +1,11 @@
+[package]
+name = "advent_13"
+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/13/input b/13/input
new file mode 100644
index 0000000..1425f20
--- /dev/null
+++ b/13/input
@@ -0,0 +1,2 @@
+1001171
+17,x,x,x,x,x,x,41,x,x,x,37,x,x,x,x,x,367,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,19,x,x,x,23,x,x,x,x,x,29,x,613,x,x,x,x,x,x,x,x,x,x,x,x,13
diff --git a/13/input.small b/13/input.small
new file mode 100644
index 0000000..d76f619
--- /dev/null
+++ b/13/input.small
@@ -0,0 +1,2 @@
+939
+7,13,x,x,59,x,31,19
diff --git a/13/src/main.rs b/13/src/main.rs
new file mode 100644
index 0000000..81ac498
--- /dev/null
+++ b/13/src/main.rs
@@ -0,0 +1,82 @@
+use advent_lib::prelude::*;
+
+use std::convert::TryFrom;
+
+
+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 timestamp = input[0].parse::<i64>()?;
+  let mut busses: Vec<Option<i64>> = Vec::new();
+
+  for word in input[1].split(',') {
+    if word == "x" {
+      busses.push(None);
+    } else {
+      busses.push(Some(word.parse::<i64>().unwrap()));
+    }
+  }
+
+  let mut best_bus = 0;
+  let mut best_delay = timestamp;
+
+  for bus in &busses {
+    match bus {
+      Some(frequency) => {
+        let delay = frequency - (timestamp % frequency);
+        if delay < best_delay {
+          best_bus = *frequency;
+          best_delay = delay;
+        }
+      },
+      None => { },
+    }
+  }
+
+  println!("{}", best_bus * best_delay);
+
+  let mut frequency_so_far = 0;
+  let mut offset_so_far = 0;
+  let mut skip_from_previous_bus = 0;
+
+  for i in 0 .. busses.len() {
+    match busses[i] {
+      Some(frequency) => {
+        if i == 0 {
+          frequency_so_far = frequency;
+          offset_so_far = 0;
+        } else {
+          // frequency_so_far * a + skip_from_previous_bus = frequency * b
+          let mut proposed_offset = offset_so_far + skip_from_previous_bus;
+          loop {
+            if proposed_offset % frequency == 0 {
+              offset_so_far = proposed_offset;
+              frequency_so_far *= frequency;
+              break;
+            } else {
+              proposed_offset += frequency_so_far;
+            }
+          }
+        }
+
+        skip_from_previous_bus = 1;
+      },
+      None => {
+        skip_from_previous_bus += 1;
+      },
+    }
+  }
+  offset_so_far -= i64::try_from(busses.len()).unwrap() - 1;
+
+  println!("{}", offset_so_far);
+
+  Ok(())
+}
+
diff --git a/13/tests/main.rs b/13/tests/main.rs
new file mode 100644
index 0000000..6dbc573
--- /dev/null
+++ b/13/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_13")?;
+
+  command.arg("input");
+  command.assert().success().stdout("1835\n247086664214628\n");
+
+  Ok(())
+}
+
diff --git a/Cargo.lock b/Cargo.lock
index 5840f89..c48bf48 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -99,6 +99,14 @@ dependencies = [
 ]
 
 [[package]]
+name = "advent_13"
+version = "0.1.0"
+dependencies = [
+ "advent_lib",
+ "assert_cmd",
+]
+
+[[package]]
 name = "advent_lib"
 version = "0.1.0"
 
diff --git a/Cargo.toml b/Cargo.toml
index 4eac77e..60b1b36 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -13,4 +13,5 @@ members = [
   "10",
   "11",
   "12",
+  "13",
 ]