diff options
Diffstat (limited to '2016/aoc16')
-rw-r--r-- | 2016/aoc16/Cargo.lock | 4 | ||||
-rw-r--r-- | 2016/aoc16/Cargo.toml | 6 | ||||
-rw-r--r-- | 2016/aoc16/src/main.rs | 68 |
3 files changed, 78 insertions, 0 deletions
diff --git a/2016/aoc16/Cargo.lock b/2016/aoc16/Cargo.lock new file mode 100644 index 0000000..af71beb --- /dev/null +++ b/2016/aoc16/Cargo.lock @@ -0,0 +1,4 @@ +[root] +name = "aoc16" +version = "0.1.0" + diff --git a/2016/aoc16/Cargo.toml b/2016/aoc16/Cargo.toml new file mode 100644 index 0000000..3e56892 --- /dev/null +++ b/2016/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/2016/aoc16/src/main.rs b/2016/aoc16/src/main.rs new file mode 100644 index 0000000..c5ecd10 --- /dev/null +++ b/2016/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")); +} + |