summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Worthe <justin.worthe@gmail.com>2016-12-16 09:59:13 +0200
committerJustin Worthe <justin.worthe@gmail.com>2016-12-16 09:59:13 +0200
commit370c7bd70a5257e0b30189f150d0ee882bbac7c2 (patch)
treeb38a12f9a0c53c020919904e142f54b09d9b60d6
parentb835d5cc786f284c1f1752fb01c135b267f9bb87 (diff)
AOC16
-rw-r--r--aoc16/Cargo.lock4
-rw-r--r--aoc16/Cargo.toml6
-rw-r--r--aoc16/src/main.rs68
3 files changed, 78 insertions, 0 deletions
diff --git a/aoc16/Cargo.lock b/aoc16/Cargo.lock
new file mode 100644
index 0000000..af71beb
--- /dev/null
+++ b/aoc16/Cargo.lock
@@ -0,0 +1,4 @@
+[root]
+name = "aoc16"
+version = "0.1.0"
+
diff --git a/aoc16/Cargo.toml b/aoc16/Cargo.toml
new file mode 100644
index 0000000..3e56892
--- /dev/null
+++ b/aoc16/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "aoc16"
+version = "0.1.0"
+authors = ["Justin Worthe <justin.worthe@gmail.com>"]
+
+[dependencies]
diff --git a/aoc16/src/main.rs b/aoc16/src/main.rs
new file mode 100644
index 0000000..c5ecd10
--- /dev/null
+++ b/aoc16/src/main.rs
@@ -0,0 +1,68 @@
+fn main() {
+ let data = initial();
+ let expanded = expand_to_size(data, 35651584);
+ let check = checksum(expanded);
+ print(&check);
+}
+
+fn initial() -> Vec<bool> {
+ to_bit_vec("10001001100000001")
+}
+
+fn to_bit_vec(input: &str) -> Vec<bool> {
+ input.chars().map(|c| c == '1').collect()
+}
+
+fn expand(a: Vec<bool>) -> Vec<bool> {
+ let mut b = a.clone();
+ b.reverse();
+ b = b.iter().map(|x| !x).collect();
+
+ let mut out = a.clone();
+ out.push(false);
+ out.append(&mut b);
+ out
+}
+
+fn expand_to_size(init: Vec<bool>, size: usize) -> Vec<bool> {
+ let mut out = init.clone();
+ while out.len() < size {
+ out = expand(out);
+ }
+ out.truncate(size);
+ out
+}
+
+fn checksum(data: Vec<bool>) -> Vec<bool> {
+ let mut check: Vec<bool> = data.chunks(2).map(|x| x[0] == x[1]).collect();
+ if check.len() % 2 == 0 {
+ check = checksum(check);
+ }
+ check
+}
+
+fn print(data: &Vec<bool>) {
+ for &bit in data {
+ print!("{}", if bit {'1'} else {'0'});
+ }
+ println!("");
+}
+
+#[test]
+fn test_expand() {
+ assert_eq!(expand(to_bit_vec("1")), to_bit_vec("100"));
+ assert_eq!(expand(to_bit_vec("0")), to_bit_vec("001"));
+ assert_eq!(expand(to_bit_vec("11111")), to_bit_vec("11111000000"));
+ assert_eq!(expand(to_bit_vec("111100001010")), to_bit_vec("1111000010100101011110000"));
+}
+
+#[test]
+fn test_expand_to_size() {
+ assert_eq!(expand_to_size(to_bit_vec("10000"), 20), to_bit_vec("10000011110010000111"));
+}
+
+#[test]
+fn test_checksum() {
+ assert_eq!(checksum(to_bit_vec("10000011110010000111")), to_bit_vec("01100"));
+}
+