From 370c7bd70a5257e0b30189f150d0ee882bbac7c2 Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Fri, 16 Dec 2016 09:59:13 +0200 Subject: AOC16 --- aoc16/Cargo.lock | 4 ++++ aoc16/Cargo.toml | 6 +++++ aoc16/src/main.rs | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 aoc16/Cargo.lock create mode 100644 aoc16/Cargo.toml create mode 100644 aoc16/src/main.rs 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 "] + +[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 { + to_bit_vec("10001001100000001") +} + +fn to_bit_vec(input: &str) -> Vec { + input.chars().map(|c| c == '1').collect() +} + +fn expand(a: Vec) -> Vec { + 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, size: usize) -> Vec { + let mut out = init.clone(); + while out.len() < size { + out = expand(out); + } + out.truncate(size); + out +} + +fn checksum(data: Vec) -> Vec { + let mut check: Vec = data.chunks(2).map(|x| x[0] == x[1]).collect(); + if check.len() % 2 == 0 { + check = checksum(check); + } + check +} + +fn print(data: &Vec) { + 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")); +} + -- cgit v1.2.3