-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e136c72
commit 71e6b61
Showing
8 changed files
with
309 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
reverse positions 1 through 6 | ||
rotate based on position of letter a | ||
swap position 4 with position 1 | ||
reverse positions 1 through 5 | ||
move position 5 to position 7 | ||
swap position 4 with position 0 | ||
swap position 4 with position 6 | ||
rotate based on position of letter a | ||
swap position 0 with position 2 | ||
move position 5 to position 2 | ||
move position 7 to position 1 | ||
swap letter d with letter c | ||
swap position 5 with position 3 | ||
reverse positions 3 through 7 | ||
rotate based on position of letter d | ||
swap position 7 with position 5 | ||
rotate based on position of letter f | ||
swap position 4 with position 1 | ||
swap position 3 with position 6 | ||
reverse positions 4 through 7 | ||
rotate based on position of letter c | ||
move position 0 to position 5 | ||
swap position 7 with position 4 | ||
rotate based on position of letter f | ||
reverse positions 1 through 3 | ||
move position 5 to position 3 | ||
rotate based on position of letter g | ||
reverse positions 2 through 5 | ||
rotate right 0 steps | ||
rotate left 0 steps | ||
swap letter f with letter b | ||
rotate based on position of letter h | ||
move position 1 to position 3 | ||
reverse positions 3 through 6 | ||
rotate based on position of letter h | ||
swap position 4 with position 3 | ||
swap letter b with letter h | ||
swap letter a with letter h | ||
reverse positions 1 through 6 | ||
swap position 3 with position 6 | ||
swap letter e with letter d | ||
swap letter e with letter h | ||
swap position 1 with position 5 | ||
rotate based on position of letter a | ||
reverse positions 4 through 5 | ||
swap position 0 with position 4 | ||
reverse positions 0 through 3 | ||
move position 7 to position 2 | ||
swap letter e with letter c | ||
swap position 3 with position 4 | ||
rotate left 3 steps | ||
rotate left 7 steps | ||
rotate based on position of letter e | ||
reverse positions 5 through 6 | ||
move position 1 to position 5 | ||
move position 1 to position 2 | ||
rotate left 1 step | ||
move position 7 to position 6 | ||
rotate left 0 steps | ||
reverse positions 5 through 6 | ||
reverse positions 3 through 7 | ||
swap letter d with letter e | ||
rotate right 3 steps | ||
swap position 2 with position 1 | ||
swap position 5 with position 7 | ||
swap letter h with letter d | ||
swap letter c with letter d | ||
rotate based on position of letter d | ||
swap letter d with letter g | ||
reverse positions 0 through 1 | ||
rotate right 0 steps | ||
swap position 2 with position 3 | ||
rotate left 4 steps | ||
rotate left 5 steps | ||
move position 7 to position 0 | ||
rotate right 1 step | ||
swap letter g with letter f | ||
rotate based on position of letter a | ||
rotate based on position of letter b | ||
swap letter g with letter e | ||
rotate right 4 steps | ||
rotate based on position of letter h | ||
reverse positions 3 through 5 | ||
swap letter h with letter e | ||
swap letter g with letter a | ||
rotate based on position of letter c | ||
reverse positions 0 through 4 | ||
rotate based on position of letter e | ||
reverse positions 4 through 7 | ||
rotate left 4 steps | ||
swap position 0 with position 6 | ||
reverse positions 1 through 6 | ||
rotate left 2 steps | ||
swap position 5 with position 3 | ||
swap letter b with letter d | ||
swap letter b with letter d | ||
rotate based on position of letter d | ||
rotate based on position of letter c | ||
rotate based on position of letter h | ||
move position 4 to position 7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
use itertools::Itertools; | ||
use std::str; | ||
|
||
#[derive(Debug)] | ||
enum Instruction { | ||
SwapPosition(usize, usize), | ||
SwapLetter(char, char), | ||
RotateLeft(usize), | ||
RotateRight(usize), | ||
RotateBasedOnLetter(char), | ||
ReversePositions(usize, usize), | ||
MovePosition(usize, usize), | ||
} | ||
|
||
impl Instruction { | ||
fn new(input: &str) -> Self { | ||
let parts = input.split_whitespace().collect_vec(); | ||
match (parts[0], parts[1]) { | ||
("swap", "position") => { | ||
let lhs = parts[2].parse::<usize>().unwrap(); | ||
let rhs = parts[5].parse::<usize>().unwrap(); | ||
Instruction::SwapPosition(lhs, rhs) | ||
} | ||
("swap", "letter") => { | ||
let lhs = parts[2].as_bytes()[0] as char; | ||
let rhs = parts[5].as_bytes()[0] as char; | ||
Instruction::SwapLetter(lhs, rhs) | ||
} | ||
("rotate", "left") => { | ||
let steps = parts[2].parse::<usize>().unwrap(); | ||
Instruction::RotateLeft(steps) | ||
} | ||
("rotate", "right") => { | ||
let steps = parts[2].parse::<usize>().unwrap(); | ||
Instruction::RotateRight(steps) | ||
} | ||
("rotate", "based") => { | ||
let chr = parts[6].as_bytes()[0] as char; | ||
Instruction::RotateBasedOnLetter(chr) | ||
} | ||
("reverse", "positions") => { | ||
let lhs = parts[2].parse::<usize>().unwrap(); | ||
let rhs = parts[4].parse::<usize>().unwrap(); | ||
Instruction::ReversePositions(lhs, rhs) | ||
} | ||
("move", "position") => { | ||
let lhs = parts[2].parse::<usize>().unwrap(); | ||
let rhs = parts[5].parse::<usize>().unwrap(); | ||
Instruction::MovePosition(lhs, rhs) | ||
} | ||
_ => panic!("Unknown instruction: {}", input), | ||
} | ||
} | ||
|
||
fn execute_rev(self, mut password: Vec<char>) -> Vec<char> { | ||
match self { | ||
Instruction::RotateLeft(steps) => password.rotate_right(steps), | ||
Instruction::RotateRight(steps) => password.rotate_left(steps), | ||
Instruction::MovePosition(lhs, rhs) => { | ||
let extract = password.remove(rhs); | ||
password.insert(lhs, extract); | ||
} | ||
Instruction::RotateBasedOnLetter(chr) => { | ||
for rotate in 0..password.len() { | ||
let mut tester = password.clone(); | ||
tester.rotate_right(rotate); | ||
let mut index = tester.iter().position(|c| *c == chr).unwrap(); | ||
index += if index >= 4 { 2 } else { 1 }; | ||
index %= password.len(); | ||
tester.rotate_right(index); | ||
if tester == password { | ||
password.rotate_left(index); | ||
break; | ||
} | ||
} | ||
} | ||
_ => { | ||
password = self.execute(password); | ||
} | ||
}; | ||
password | ||
} | ||
|
||
fn execute(self, mut password: Vec<char>) -> Vec<char> { | ||
match self { | ||
Instruction::SwapPosition(lhs, rhs) => password.swap(lhs, rhs), | ||
Instruction::SwapLetter(lhs, rhs) => { | ||
for idx in 0..password.len() { | ||
if password[idx] == lhs { | ||
password[idx] = rhs; | ||
} else if password[idx] == rhs { | ||
password[idx] = lhs; | ||
} | ||
} | ||
} | ||
Instruction::RotateLeft(steps) => password.rotate_left(steps), | ||
Instruction::RotateRight(steps) => password.rotate_right(steps), | ||
Instruction::RotateBasedOnLetter(chr) => { | ||
let mut index = password.iter().position(|c| *c == chr).unwrap(); | ||
index += if index >= 4 { 2 } else { 1 }; | ||
index %= password.len(); | ||
password.rotate_right(index); | ||
} | ||
Instruction::ReversePositions(mut lhs, mut rhs) => { | ||
while lhs < rhs { | ||
password.swap(lhs, rhs); | ||
lhs += 1; | ||
rhs -= 1; | ||
} | ||
} | ||
Instruction::MovePosition(lhs, rhs) => { | ||
let extract = password.remove(lhs); | ||
password.insert(rhs, extract); | ||
} | ||
} | ||
password | ||
} | ||
} | ||
|
||
pub fn day_21_v1(input: impl Into<String>) -> String { | ||
let mut password = "abcdefgh".chars().collect::<Vec<_>>(); | ||
let instructions = input | ||
.into() | ||
.lines() | ||
.map(|line| Instruction::new(line)) | ||
.collect_vec(); | ||
for instruction in instructions { | ||
password = instruction.execute(password); | ||
} | ||
|
||
password.iter().collect::<String>() | ||
} | ||
|
||
pub fn day_21_v2(input: impl Into<String>) -> String { | ||
let mut password = "fbgdceah".chars().collect::<Vec<_>>(); | ||
let mut instructions = input | ||
.into() | ||
.lines() | ||
.map(|line| Instruction::new(line)) | ||
.collect_vec(); | ||
instructions.reverse(); | ||
for instruction in instructions { | ||
password = instruction.execute_rev(password); | ||
} | ||
|
||
password.iter().collect::<String>() | ||
} | ||
|
||
solvable!(day_21, day_21_v1, day_21_v2, String); | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
const SAMPLE: &str = "swap position 4 with position 0\n\ | ||
swap letter d with letter b\n\ | ||
reverse positions 0 through 4\n\ | ||
rotate left 1 step\n\ | ||
move position 1 to position 4\n\ | ||
move position 3 to position 0\n\ | ||
rotate based on position of letter b\n\ | ||
rotate based on position of letter d"; | ||
|
||
#[test] | ||
fn can_interpret_code() { | ||
let mut password = "abcde".chars().collect::<Vec<_>>(); | ||
let instructions = SAMPLE | ||
.lines() | ||
.map(|line| Instruction::new(line)) | ||
.collect_vec(); | ||
for instruction in instructions { | ||
password = instruction.execute(password); | ||
} | ||
assert_eq!(password.iter().collect::<String>(), "decab"); | ||
} | ||
} |