summary refs log tree commit diff
diff options
context:
space:
mode:
authorIrene Knapp <ireneista@gmail.com>2020-12-08 21:32:45 -0800
committerIrene Knapp <ireneista@gmail.com>2020-12-08 21:32:45 -0800
commite9f5cbe12013d3c41a6c438e767025aba66fe53b (patch)
tree805caf354dac7d5f692d2f4ac355d6669494bdfb
parentd8b56bc04ae56619687ada8fddbdf61ddd588262 (diff)
09
-rw-r--r--09/Cargo.toml11
-rw-r--r--09/src/main.rs83
-rw-r--r--09/tests/main.rs14
-rw-r--r--Cargo.lock8
-rw-r--r--Cargo.toml1
5 files changed, 117 insertions, 0 deletions
diff --git a/09/Cargo.toml b/09/Cargo.toml
new file mode 100644
index 0000000..a40c938
--- /dev/null
+++ b/09/Cargo.toml
@@ -0,0 +1,11 @@
+[package]
+name = "advent_09"
+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/09/src/main.rs b/09/src/main.rs
new file mode 100644
index 0000000..ca978cb
--- /dev/null
+++ b/09/src/main.rs
@@ -0,0 +1,83 @@
+use advent_lib::prelude::*;
+
+
+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 ciphertext: Vec<i64> = Vec::new();
+  for line in &input {
+    ciphertext.push(line.parse::<i64>().unwrap());
+  }
+
+  let mut first_key = 0;
+
+  for i in 25 .. ciphertext.len() {
+    let sum = ciphertext[i];
+    let mut found_addends = false;
+
+    for j in 0 .. 25 {
+      let a = ciphertext[i - 25 + j];
+      let b = sum - a;
+      for k in j + 1 .. 25 {
+        if ciphertext[i - 25 + k] == b {
+          found_addends = true;
+          break;
+        }
+      }
+      if found_addends {
+        break;
+      }
+    }
+
+    if !found_addends {
+      println!("{}", sum);
+      first_key = sum;
+      break;
+    }
+  }
+
+  for run_length in 2 .. ciphertext.len() {
+    let mut sum = 0;
+    let found_solution = false;
+
+    for i in 0 .. ciphertext.len() - run_length {
+      sum += ciphertext[i];
+
+      if i >= run_length {
+        sum -= ciphertext[i - run_length];
+      }
+
+      if sum == first_key {
+        let mut min = 0;
+        let mut max = 0;
+
+        for j in 0 .. run_length {
+          let item = ciphertext[i - run_length + j + 1];
+          if j == 0 || item < min {
+            min = item;
+          }
+          if j == 0 || item > max {
+            max = item;
+          }
+        }
+
+        println!("{}", min + max);
+        break;
+      }
+    }
+
+    if found_solution {
+      break;
+    }
+  }
+
+  Ok(())
+}
+
diff --git a/09/tests/main.rs b/09/tests/main.rs
new file mode 100644
index 0000000..e34e4b6
--- /dev/null
+++ b/09/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_09")?;
+
+  command.arg("input");
+  command.assert().success().stdout("69316178\n9351526\n");
+
+  Ok(())
+}
+
diff --git a/Cargo.lock b/Cargo.lock
index 9a04392..4be6ce1 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -67,6 +67,14 @@ dependencies = [
 ]
 
 [[package]]
+name = "advent_09"
+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 e72d2a8..f10de33 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -9,4 +9,5 @@ members = [
   "06",
   "07",
   "08",
+  "09",
 ]