summaryrefslogtreecommitdiff
path: root/2016/aoc21
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2022-04-19 20:22:56 +0200
committerJustin Wernick <justin@worthe-it.co.za>2022-04-19 20:23:15 +0200
commit174772b5b8d9f5bf5e3c8e8152adfd89f0e83f6b (patch)
treea003b748ee939b30a2bcd3caf2378228baa304c1 /2016/aoc21
parentfd75b3fb95ad049b0025cb8fc0b3459b8f872d61 (diff)
Refile for merging repos
Diffstat (limited to '2016/aoc21')
-rw-r--r--2016/aoc21/Cargo.lock98
-rw-r--r--2016/aoc21/Cargo.toml7
-rw-r--r--2016/aoc21/input.txt100
-rw-r--r--2016/aoc21/src/main.rs233
4 files changed, 438 insertions, 0 deletions
diff --git a/2016/aoc21/Cargo.lock b/2016/aoc21/Cargo.lock
new file mode 100644
index 0000000..9a1075b
--- /dev/null
+++ b/2016/aoc21/Cargo.lock
@@ -0,0 +1,98 @@
+[root]
+name = "aoc21"
+version = "0.1.0"
+dependencies = [
+ "regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "aho-corasick"
+version = "0.5.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "kernel32-sys"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
+ "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "libc"
+version = "0.2.18"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "memchr"
+version = "0.1.11"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "regex"
+version = "0.1.80"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "aho-corasick 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
+ "memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)",
+ "regex-syntax 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)",
+ "thread_local 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
+ "utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "regex-syntax"
+version = "0.3.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "thread-id"
+version = "2.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "thread_local"
+version = "0.2.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "utf8-ranges"
+version = "0.1.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "winapi"
+version = "0.2.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "winapi-build"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[metadata]
+"checksum aho-corasick 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ca972c2ea5f742bfce5687b9aef75506a764f61d37f8f649047846a9686ddb66"
+"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
+"checksum libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)" = "a51822fc847e7a8101514d1d44e354ba2ffa7d4c194dcab48870740e327cac70"
+"checksum memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d8b629fb514376c675b98c1421e80b151d3817ac42d7c667717d282761418d20"
+"checksum regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)" = "4fd4ace6a8cf7860714a2c2280d6c1f7e6a413486c13298bbc86fd3da019402f"
+"checksum regex-syntax 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "f9ec002c35e86791825ed294b50008eea9ddfc8def4420124fbc6b08db834957"
+"checksum thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a9539db560102d1cef46b8b78ce737ff0bb64e7e18d35b2a5688f7d097d0ff03"
+"checksum thread_local 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "8576dbbfcaef9641452d5cf0df9b0e7eeab7694956dd33bb61515fb8f18cfdd5"
+"checksum utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a1ca13c08c41c9c3e04224ed9ff80461d97e121589ff27c753a16cb10830ae0f"
+"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
+"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"
diff --git a/2016/aoc21/Cargo.toml b/2016/aoc21/Cargo.toml
new file mode 100644
index 0000000..bc0d468
--- /dev/null
+++ b/2016/aoc21/Cargo.toml
@@ -0,0 +1,7 @@
+[package]
+name = "aoc21"
+version = "0.1.0"
+authors = ["Justin Worthe <justin.worthe@gmail.com>"]
+
+[dependencies]
+regex = "0.1" \ No newline at end of file
diff --git a/2016/aoc21/input.txt b/2016/aoc21/input.txt
new file mode 100644
index 0000000..ebd7d52
--- /dev/null
+++ b/2016/aoc21/input.txt
@@ -0,0 +1,100 @@
+rotate based on position of letter d
+move position 1 to position 6
+swap position 3 with position 6
+rotate based on position of letter c
+swap position 0 with position 1
+rotate right 5 steps
+rotate left 3 steps
+rotate based on position of letter b
+swap position 0 with position 2
+rotate based on position of letter g
+rotate left 0 steps
+reverse positions 0 through 3
+rotate based on position of letter a
+rotate based on position of letter h
+rotate based on position of letter a
+rotate based on position of letter g
+rotate left 5 steps
+move position 3 to position 7
+rotate right 5 steps
+rotate based on position of letter f
+rotate right 7 steps
+rotate based on position of letter a
+rotate right 6 steps
+rotate based on position of letter a
+swap letter c with letter f
+reverse positions 2 through 6
+rotate left 1 step
+reverse positions 3 through 5
+rotate based on position of letter f
+swap position 6 with position 5
+swap letter h with letter e
+move position 1 to position 3
+swap letter c with letter h
+reverse positions 4 through 7
+swap letter f with letter h
+rotate based on position of letter f
+rotate based on position of letter g
+reverse positions 3 through 4
+rotate left 7 steps
+swap letter h with letter a
+rotate based on position of letter e
+rotate based on position of letter f
+rotate based on position of letter g
+move position 5 to position 0
+rotate based on position of letter c
+reverse positions 3 through 6
+rotate right 4 steps
+move position 1 to position 2
+reverse positions 3 through 6
+swap letter g with letter a
+rotate based on position of letter d
+rotate based on position of letter a
+swap position 0 with position 7
+rotate left 7 steps
+rotate right 2 steps
+rotate right 6 steps
+rotate based on position of letter b
+rotate right 2 steps
+swap position 7 with position 4
+rotate left 4 steps
+rotate left 3 steps
+swap position 2 with position 7
+move position 5 to position 4
+rotate right 3 steps
+rotate based on position of letter g
+move position 1 to position 2
+swap position 7 with position 0
+move position 4 to position 6
+move position 3 to position 0
+rotate based on position of letter f
+swap letter g with letter d
+swap position 1 with position 5
+reverse positions 0 through 2
+swap position 7 with position 3
+rotate based on position of letter g
+swap letter c with letter a
+rotate based on position of letter g
+reverse positions 3 through 5
+move position 6 to position 3
+swap letter b with letter e
+reverse positions 5 through 6
+move position 6 to position 7
+swap letter a with letter e
+swap position 6 with position 2
+move position 4 to position 5
+rotate left 5 steps
+swap letter a with letter d
+swap letter e with letter g
+swap position 3 with position 7
+reverse positions 0 through 5
+swap position 5 with position 7
+swap position 1 with position 7
+swap position 1 with position 7
+rotate right 7 steps
+swap letter f with letter a
+reverse positions 0 through 7
+rotate based on position of letter d
+reverse positions 2 through 4
+swap position 7 with position 1
+swap letter a with letter h
diff --git a/2016/aoc21/src/main.rs b/2016/aoc21/src/main.rs
new file mode 100644
index 0000000..1366f1e
--- /dev/null
+++ b/2016/aoc21/src/main.rs
@@ -0,0 +1,233 @@
+extern crate regex;
+
+use std::io::BufReader;
+use std::io::prelude::*;
+use std::fs::File;
+use std::collections::VecDeque;
+
+use regex::Regex;
+
+fn main() {
+ let password = "abcdefgh".to_string();
+ let program = read_file();
+ let scrambled = scramble_password(password, &program);
+ println!("Scrambled password is {}", scrambled);
+ let unscrambled = unscramble_password(scrambled, &program);
+ println!("Which unscrambles to {}", unscrambled);
+
+ let other_scrambled = "fbgdceah".to_string();
+ let other_unscrambled = unscramble_password(other_scrambled, &program);
+
+ println!("Decrypted easter bunny password is {}", other_unscrambled);
+}
+
+fn read_file() -> Vec<String> {
+ let file = BufReader::new(File::open("input.txt").unwrap());
+ file.lines()
+ .map(|line| line.unwrap().trim().to_string())
+ .filter(|line| line.len() > 0)
+ .collect()
+}
+
+fn scramble_password(password: String, program: &Vec<String>) -> String {
+ let mut pass = password.chars().collect::<VecDeque<_>>();
+
+ let instructions = scramble_instruction_set();
+
+ for line in program {
+ for &(ref reg, ref func) in &instructions {
+ let captures = reg.captures(line.as_ref());
+ if captures.is_some() {
+ func(&mut pass, captures.unwrap());
+ }
+ }
+ }
+
+ pass.iter().cloned().collect()
+}
+
+fn unscramble_password(password: String, program: &Vec<String>) -> String {
+ let mut pass = password.chars().collect::<VecDeque<_>>();
+
+ let instructions = unscramble_instruction_set();
+
+ let mut backwards_program = program.clone();
+ backwards_program.reverse();
+ for line in backwards_program {
+ for &(ref reg, ref func) in &instructions {
+ let captures = reg.captures(line.as_ref());
+ if captures.is_some() {
+ func(&mut pass, captures.unwrap());
+ }
+ }
+ }
+
+ pass.iter().cloned().collect()
+}
+
+fn scramble_instruction_set() -> Vec<(Regex, fn(&mut VecDeque<char>, regex::Captures))> {
+ let mut instructions : Vec<(Regex, fn(&mut VecDeque<char>, regex::Captures))> = Vec::new();
+ instructions.push((Regex::new(r"swap position (\d+) with position (\d+)").unwrap(), swap_pos));
+ instructions.push((Regex::new(r"swap letter ([a-z]) with letter ([a-z])").unwrap(), swap_let));
+ instructions.push((Regex::new(r"rotate left (\d+) step").unwrap(), rotate_left));
+ instructions.push((Regex::new(r"rotate right (\d+) step").unwrap(), rotate_right));
+ instructions.push((Regex::new(r"rotate based on position of letter ([a-z])").unwrap(), rotate_pos));
+ instructions.push((Regex::new(r"reverse positions (\d+) through (\d+)").unwrap(), reverse_sub));
+ instructions.push((Regex::new(r"move position (\d+) to position (\d+)").unwrap(), move_pos));
+
+ instructions
+}
+
+fn unscramble_instruction_set() -> Vec<(Regex, fn(&mut VecDeque<char>, regex::Captures))> {
+ let mut instructions : Vec<(Regex, fn(&mut VecDeque<char>, regex::Captures))> = Vec::new();
+ instructions.push((Regex::new(r"swap position (\d+) with position (\d+)").unwrap(), swap_pos));
+ instructions.push((Regex::new(r"swap letter ([a-z]) with letter ([a-z])").unwrap(), swap_let));
+ instructions.push((Regex::new(r"rotate left (\d+) step").unwrap(), rotate_right));
+ instructions.push((Regex::new(r"rotate right (\d+) step").unwrap(), rotate_left));
+ instructions.push((Regex::new(r"rotate based on position of letter ([a-z])").unwrap(), undo_rotate_pos));
+ instructions.push((Regex::new(r"reverse positions (\d+) through (\d+)").unwrap(), reverse_sub));
+ instructions.push((Regex::new(r"move position (\d+) to position (\d+)").unwrap(), undo_move_pos));
+
+ instructions
+}
+
+fn swap_pos(pass: &mut VecDeque<char>, cap: regex::Captures) {
+ let src = cap.at(1).unwrap().parse::<usize>().unwrap();
+ let dest = cap.at(2).unwrap().parse::<usize>().unwrap();
+ pass.swap(src, dest);
+}
+
+fn swap_let(pass: &mut VecDeque<char>, cap: regex::Captures) {
+ let src = cap.at(1).unwrap().chars().next().unwrap();
+ let dest = cap.at(2).unwrap().chars().next().unwrap();
+ let src_position = pass.iter().position(|&c| c == src);
+ let dest_position = pass.iter().position(|&c| c == dest);
+ match (src_position, dest_position) {
+ (Some(src_pos), Some(dest_pos)) => {
+ pass.swap(src_pos, dest_pos);
+ },
+ _ => {}
+ };
+}
+
+fn rotate_left(pass: &mut VecDeque<char>, cap: regex::Captures) {
+ let steps = cap.at(1).unwrap().parse::<usize>().unwrap();
+ for _ in 0..steps {
+ let c = pass.pop_front().unwrap();
+ pass.push_back(c);
+ }
+}
+
+fn rotate_right(pass: &mut VecDeque<char>, cap: regex::Captures) {
+ let steps = cap.at(1).unwrap().parse::<usize>().unwrap();
+ for _ in 0..steps {
+ let c = pass.pop_back().unwrap();
+ pass.push_front(c);
+ }
+}
+
+fn rotate_pos(pass: &mut VecDeque<char>, cap: regex::Captures) {
+ let letter = cap.at(1).unwrap().chars().next().unwrap();
+ let pos = pass.iter().position(|&c| c == letter).unwrap();
+ let steps = 1 + pos + if pos >=4 { 1 } else { 0 };
+ for _ in 0..steps {
+ let c = pass.pop_back().unwrap();
+ pass.push_front(c);
+ }
+}
+
+fn move_pos(pass: &mut VecDeque<char>, cap: regex::Captures) {
+ let src = cap.at(1).unwrap().parse::<usize>().unwrap();
+ let dest = cap.at(2).unwrap().parse::<usize>().unwrap();
+ let c = pass.remove(src).unwrap();
+ pass.insert(dest, c);
+}
+
+fn reverse_sub(pass: &mut VecDeque<char>, cap: regex::Captures) {
+ let mut start = cap.at(1).unwrap().parse::<usize>().unwrap();
+ let mut end = cap.at(2).unwrap().parse::<usize>().unwrap();
+ while start < end {
+ pass.swap(start, end);
+ start += 1;
+ end -= 1;
+ }
+}
+
+fn undo_rotate_pos(pass: &mut VecDeque<char>, cap: regex::Captures) {
+ let letter = cap.at(1).unwrap().chars().next().unwrap();
+ let dest_pos = pass.iter().position(|&c| c == letter).unwrap();
+ let mut pos = dest_pos;
+
+ loop {
+ let c = pass.pop_front().unwrap();
+ pass.push_back(c);
+ pos -= 1;
+ let steps = 1 + pos + if pos >=4 { 1 } else { 0 };
+ if (pos + steps) % pass.len() == dest_pos {
+ break;
+ }
+ }
+}
+
+fn undo_move_pos(pass: &mut VecDeque<char>, cap: regex::Captures) {
+ let src = cap.at(1).unwrap().parse::<usize>().unwrap();
+ let dest = cap.at(2).unwrap().parse::<usize>().unwrap();
+ let c = pass.remove(dest).unwrap();
+ pass.insert(src, c);
+}
+
+#[test]
+fn swap_pos_example() {
+ let instructions = vec!("swap position 4 with position 0".to_string());
+ let password = "abcde".to_string();
+ assert_eq!("ebcda".to_string(), scramble_password(password, instructions));
+}
+
+#[test]
+fn swap_let_example() {
+ let instructions = vec!("swap letter d with letter b".to_string());
+ let password = "ebcda".to_string();
+ assert_eq!("edcba".to_string(), scramble_password(password, instructions));
+}
+
+#[test]
+fn reverse_example() {
+ let instructions = vec!("reverse positions 0 through 4".to_string());
+ let password = "edcba".to_string();
+ assert_eq!("abcde".to_string(), scramble_password(password, instructions));
+}
+
+#[test]
+fn rotate_left_example() {
+ let instructions = vec!("rotate left 1 step".to_string());
+ let password = "abcde".to_string();
+ assert_eq!("bcdea".to_string(), scramble_password(password, instructions));
+}
+
+#[test]
+fn move_example() {
+ let instructions = vec!("move position 3 to position 0".to_string());
+ let password = "bdeac".to_string();
+ assert_eq!("abdec".to_string(), scramble_password(password, instructions));
+}
+
+#[test]
+fn rotate_pos_example_1() {
+ let instructions = vec!("rotate based on position of letter b".to_string());
+ let password = "abdec".to_string();
+ assert_eq!("ecabd".to_string(), scramble_password(password, instructions));
+}
+
+#[test]
+fn rotate_pos_example_2() {
+ let instructions = vec!("rotate based on position of letter d".to_string());
+ let password = "ecabd".to_string();
+ assert_eq!("decab".to_string(), scramble_password(password, instructions));
+}
+
+#[test]
+fn rotate_right_example() {
+ let instructions = vec!("rotate right 2 steps".to_string());
+ let password = "decab".to_string();
+ assert_eq!("abdec".to_string(), scramble_password(password, instructions));
+}