summary refs log tree commit diff
path: root/10
diff options
context:
space:
mode:
authorIrene Knapp <ireneista@gmail.com>2020-12-09 22:32:05 -0800
committerIrene Knapp <ireneista@gmail.com>2020-12-09 22:32:05 -0800
commit99ffb910798d46b8c8a6d91aa8ff2b06fc4313f8 (patch)
tree75cc47c2e5467f328f8675f6f9b8ec7ed311ce30 /10
parente9f5cbe12013d3c41a6c438e767025aba66fe53b (diff)
10
Diffstat (limited to '10')
-rw-r--r--10/Cargo.toml11
-rw-r--r--10/input114
-rw-r--r--10/input.medium31
-rw-r--r--10/input.small11
-rw-r--r--10/src/main.rs67
-rw-r--r--10/tests/main.rs14
6 files changed, 248 insertions, 0 deletions
diff --git a/10/Cargo.toml b/10/Cargo.toml
new file mode 100644
index 0000000..6b36e1c
--- /dev/null
+++ b/10/Cargo.toml
@@ -0,0 +1,11 @@
+[package]
+name = "advent_10"
+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/10/input b/10/input
new file mode 100644
index 0000000..5b21a23
--- /dev/null
+++ b/10/input
@@ -0,0 +1,114 @@
+74
+153
+60
+163
+112
+151
+22
+67
+43
+160
+193
+6
+2
+16
+122
+126
+32
+181
+180
+139
+20
+111
+66
+81
+12
+56
+63
+95
+90
+161
+33
+134
+31
+119
+53
+148
+104
+91
+140
+36
+144
+23
+130
+178
+146
+38
+133
+192
+131
+3
+73
+11
+62
+50
+89
+98
+103
+110
+164
+48
+80
+179
+92
+194
+86
+40
+13
+123
+68
+115
+19
+46
+77
+152
+138
+69
+49
+59
+30
+132
+9
+185
+1
+188
+171
+72
+116
+101
+61
+141
+107
+21
+47
+147
+182
+170
+39
+37
+127
+26
+102
+137
+191
+162
+172
+29
+10
+154
+157
+83
+82
+175
+145
+167
diff --git a/10/input.medium b/10/input.medium
new file mode 100644
index 0000000..e6376dc
--- /dev/null
+++ b/10/input.medium
@@ -0,0 +1,31 @@
+28
+33
+18
+42
+31
+14
+46
+20
+48
+47
+24
+23
+49
+45
+19
+38
+39
+11
+1
+32
+25
+35
+8
+17
+7
+9
+4
+2
+34
+10
+3
diff --git a/10/input.small b/10/input.small
new file mode 100644
index 0000000..ec4a03f
--- /dev/null
+++ b/10/input.small
@@ -0,0 +1,11 @@
+16
+10
+15
+5
+1
+11
+7
+19
+6
+12
+4
diff --git a/10/src/main.rs b/10/src/main.rs
new file mode 100644
index 0000000..d504711
--- /dev/null
+++ b/10/src/main.rs
@@ -0,0 +1,67 @@
+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 mut adaptors: Vec<i64> = Vec::new();
+  for line in &input {
+    adaptors.push(line.parse::<i64>().unwrap());
+  }
+  adaptors.sort();
+
+  let mut buckets = Vec::new();
+  buckets.push(0);
+  buckets.push(0);
+  buckets.push(0);
+  buckets.push(0);
+
+  for i in 0 .. adaptors.len() {
+    let mut prev_joltage = 0;
+    if i > 0 {
+      prev_joltage = adaptors[i - 1];
+    }
+    let difference = adaptors[i] - prev_joltage;
+    buckets[usize::try_from(difference).unwrap()] += 1;
+  }
+
+  buckets[3] += 1;
+
+  println!("{}", buckets[1]*buckets[3]);
+
+  let mut reversed_adaptors = Vec::new();
+  for i in 0 .. adaptors.len() {
+    reversed_adaptors.push(adaptors[adaptors.len() - i - 1]);
+  }
+  reversed_adaptors.push(0);
+
+  let mut memoized_counts: Vec<i64> = Vec::new();
+  memoized_counts.push(1);
+
+  for i in 1 .. reversed_adaptors.len() {
+    let mut count = 0;
+    for j in 1 .. i+1 {
+      let difference = reversed_adaptors[i - j] - reversed_adaptors[i];
+      if difference <= 3 {
+        count += memoized_counts[i - j];
+      } else {
+        break;
+      }
+    }
+    memoized_counts.push(count);
+  }
+
+  println!("{}", memoized_counts[memoized_counts.len()-1]);
+
+  Ok(())
+}
+
diff --git a/10/tests/main.rs b/10/tests/main.rs
new file mode 100644
index 0000000..e0b01e8
--- /dev/null
+++ b/10/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_10")?;
+
+  command.arg("input");
+  command.assert().success().stdout("3034\n259172170858496\n");
+
+  Ok(())
+}
+