Solutions for 2022, as well as 2015-2018 and 2019 up to day 11
This commit is contained in:
commit
1895197c49
722 changed files with 375457 additions and 0 deletions
8
2016/day21-scrambled_letters_and_hash/Cargo.toml
Normal file
8
2016/day21-scrambled_letters_and_hash/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "day21-scrambled_letters_and_hash"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
45
2016/day21-scrambled_letters_and_hash/challenge.txt
Normal file
45
2016/day21-scrambled_letters_and_hash/challenge.txt
Normal file
|
@ -0,0 +1,45 @@
|
|||
\--- Day 21: Scrambled Letters and Hash ---
|
||||
----------
|
||||
|
||||
The computer system you're breaking into uses a weird scrambling function to store its passwords. It shouldn't be much trouble to create your own scrambled password so you can add it to the system; you just have to implement the scrambler.
|
||||
|
||||
The scrambling function is a series of operations (the exact list is provided in your puzzle input). Starting with the password to be scrambled, apply each operation in succession to the string. The individual operations behave as follows:
|
||||
|
||||
* `swap position X with position Y` means that the letters at indexes `X` and `Y` (counting from `0`) should be *swapped*.
|
||||
* `swap letter X with letter Y` means that the letters `X` and `Y` should be *swapped* (regardless of where they appear in the string).
|
||||
* `rotate left/right X steps` means that the whole string should be *rotated*; for example, one right rotation would turn `abcd` into `dabc`.
|
||||
* `rotate based on position of letter X` means that the whole string should be *rotated to the right* based on the *index* of letter `X` (counting from `0`) as determined *before* this instruction does any rotations. Once the index is determined, rotate the string to the right one time, plus a number of times equal to that index, plus one additional time if the index was at least `4`.
|
||||
* `reverse positions X through Y` means that the span of letters at indexes `X` through `Y` (including the letters at `X` and `Y`) should be *reversed in order*.
|
||||
* `move position X to position Y` means that the letter which is at index `X` should be *removed* from the string, then *inserted* such that it ends up at index `Y`.
|
||||
|
||||
For example, suppose you start with `abcde` and perform the following operations:
|
||||
|
||||
* `swap position 4 with position 0` swaps the first and last letters, producing the input for the next step, `ebcda`.
|
||||
* `swap letter d with letter b` swaps the positions of `d` and `b`: `edcba`.
|
||||
* `reverse positions 0 through 4` causes the entire string to be reversed, producing `abcde`.
|
||||
* `rotate left 1 step` shifts all letters left one position, causing the first letter to wrap to the end of the string: `bcdea`.
|
||||
* `move position 1 to position 4` removes the letter at position `1` (`c`), then inserts it at position `4` (the end of the string): `bdeac`.
|
||||
* `move position 3 to position 0` removes the letter at position `3` (`a`), then inserts it at position `0` (the front of the string): `abdec`.
|
||||
* `rotate based on position of letter b` finds the index of letter `b` (`1`), then rotates the string right once plus a number of times equal to that index (`2`): `ecabd`.
|
||||
* `rotate based on position of letter d` finds the index of letter `d` (`4`), then rotates the string right once, plus a number of times equal to that index, plus an additional time because the index was at least `4`, for a total of `6` right rotations: `decab`.
|
||||
|
||||
After these steps, the resulting scrambled password is `decab`.
|
||||
|
||||
Now, you just need to generate a new scrambled password and you can access the system. Given the list of scrambling operations in your puzzle input, *what is the result of scrambling `abcdefgh`*?
|
||||
|
||||
Your puzzle answer was `bdfhgeca`.
|
||||
|
||||
\--- Part Two ---
|
||||
----------
|
||||
|
||||
You scrambled the password correctly, but you discover that you [can't actually modify](https://en.wikipedia.org/wiki/File_system_permissions) the [password file](https://en.wikipedia.org/wiki/Passwd) on the system. You'll need to un-scramble one of the existing passwords by reversing the scrambling process.
|
||||
|
||||
What is the un-scrambled version of the scrambled password `fbgdceah`?
|
||||
|
||||
Your puzzle answer was `gdfcabeh`.
|
||||
|
||||
Both parts of this puzzle are complete! They provide two gold stars: \*\*
|
||||
|
||||
At this point, all that is left is for you to [admire your Advent calendar](/2016).
|
||||
|
||||
If you still want to see it, you can [get your puzzle input](21/input).
|
116
2016/day21-scrambled_letters_and_hash/src/lib.rs
Normal file
116
2016/day21-scrambled_letters_and_hash/src/lib.rs
Normal file
|
@ -0,0 +1,116 @@
|
|||
enum Operation {
|
||||
SwapPos(usize, usize),
|
||||
SwapLtr(char, char),
|
||||
RotateL(usize),
|
||||
RotateR(usize),
|
||||
RotateIdx(char),
|
||||
Reverse(usize, usize),
|
||||
Move(usize, usize),
|
||||
}
|
||||
|
||||
impl Operation {
|
||||
fn parse(line: &str) -> Self {
|
||||
let components: Vec<_> = line.split(' ').collect();
|
||||
match (components[0], components[1]) {
|
||||
("swap", "position") => Self::SwapPos(components[2].parse().unwrap(), components[5].parse().unwrap()),
|
||||
("swap", "letter") => Self::SwapLtr(components[2].parse().unwrap(), components[5].parse().unwrap()),
|
||||
("rotate", "left") => Self::RotateL(components[2].parse().unwrap()),
|
||||
("rotate", "right") => Self::RotateR(components[2].parse().unwrap()),
|
||||
("rotate", "based") => Self::RotateIdx(components[6].parse().unwrap()),
|
||||
("reverse", _) => Self::Reverse(components[2].parse().unwrap(), components[4].parse().unwrap()),
|
||||
("move", _) => Self::Move(components[2].parse().unwrap(), components[5].parse().unwrap()),
|
||||
_ => panic!("Operation not recognized: {line}"),
|
||||
}
|
||||
}
|
||||
|
||||
fn perform(&self, input: &mut [u8]) {
|
||||
match self {
|
||||
Self::SwapPos(x, y) => input.swap(*x, *y),
|
||||
Self::SwapLtr(x, y) => {
|
||||
let xs: Vec<_> = input.iter().enumerate().filter(|(_, c)| **c == *x as u8).map(|(idx, _)| idx).collect();
|
||||
let ys: Vec<_> = input.iter().enumerate().filter(|(_, c)| **c == *y as u8).map(|(idx, _)| idx).collect();
|
||||
for idx in xs {
|
||||
input[idx] = *y as u8;
|
||||
}
|
||||
for idx in ys {
|
||||
input[idx] = *x as u8;
|
||||
}
|
||||
},
|
||||
Self::RotateL(x) => input.rotate_left(*x),
|
||||
Self::RotateR(x) => input.rotate_right(*x),
|
||||
Self::RotateIdx(x) => {
|
||||
let mut mid = 1 + input.iter().position(|c| *c == *x as u8).unwrap();
|
||||
if mid > 4 { mid += 1; }
|
||||
mid %= input.len();
|
||||
input.rotate_right(mid);
|
||||
},
|
||||
Self::Reverse(x, y) => input[*x..=*y].reverse(),
|
||||
Self::Move(x, y) => {
|
||||
if x<y {
|
||||
input[*x..=*y].rotate_left(1);
|
||||
} else {
|
||||
input[*y..=*x].rotate_right(1);
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_permutations(rest: &str) -> Vec<String> {
|
||||
if rest.len() == 1 {
|
||||
return Vec::from([rest.to_string()]);
|
||||
}
|
||||
let mut res = Vec::new();
|
||||
for (idx, c) in rest.chars().enumerate() {
|
||||
let mut new_rest = rest.to_string();
|
||||
new_rest.remove(idx);
|
||||
for this_result in get_permutations(&new_rest) {
|
||||
res.push(format!("{c}{this_result}"));
|
||||
}
|
||||
}
|
||||
res
|
||||
}
|
||||
|
||||
pub fn unscramble(input: &str, goal: &str) -> String {
|
||||
let permutations = get_permutations(goal);
|
||||
for permutation in permutations {
|
||||
if scramble(input, &permutation) == *goal.to_string() {
|
||||
return permutation;
|
||||
}
|
||||
}
|
||||
"No suitable permutation found.".to_string()
|
||||
}
|
||||
|
||||
pub fn scramble(input: &str, starting: &str) -> String {
|
||||
let operations: Vec<_> = input.lines().map(Operation::parse).collect();
|
||||
let mut bytes = starting.as_bytes().to_vec();
|
||||
for op in operations {
|
||||
op.perform(&mut bytes);
|
||||
}
|
||||
String::from_utf8(bytes).unwrap()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::fs::read_to_string;
|
||||
|
||||
fn read_file(name: &str) -> String {
|
||||
read_to_string(name).expect(&format!("Unable to read file: {name}")[..]).trim().to_string()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sample() {
|
||||
let sample_input = read_file("tests/sample_input");
|
||||
assert_eq!(scramble(&sample_input, "abcde"), "decab".to_string());
|
||||
assert_eq!(scramble(&sample_input, "deabc"), "decab".to_string());
|
||||
assert_eq!(unscramble(&sample_input, "decab"), "deabc".to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_challenge() {
|
||||
let challenge_input = read_file("tests/challenge_input");
|
||||
assert_eq!(scramble(&challenge_input, "abcdefgh"), "bdfhgeca".to_string());
|
||||
assert_eq!(unscramble(&challenge_input, "fbgdceah"), "gdfcabeh".to_string());
|
||||
}
|
||||
}
|
100
2016/day21-scrambled_letters_and_hash/tests/challenge_input
Normal file
100
2016/day21-scrambled_letters_and_hash/tests/challenge_input
Normal file
|
@ -0,0 +1,100 @@
|
|||
swap position 5 with position 6
|
||||
reverse positions 1 through 6
|
||||
rotate right 7 steps
|
||||
rotate based on position of letter c
|
||||
rotate right 7 steps
|
||||
reverse positions 0 through 4
|
||||
swap letter f with letter h
|
||||
reverse positions 1 through 2
|
||||
move position 1 to position 0
|
||||
rotate based on position of letter f
|
||||
move position 6 to position 3
|
||||
reverse positions 3 through 6
|
||||
rotate based on position of letter c
|
||||
rotate based on position of letter b
|
||||
move position 2 to position 4
|
||||
swap letter b with letter d
|
||||
move position 1 to position 6
|
||||
move position 7 to position 1
|
||||
swap letter f with letter c
|
||||
move position 2 to position 3
|
||||
swap position 1 with position 7
|
||||
reverse positions 3 through 5
|
||||
swap position 1 with position 4
|
||||
move position 4 to position 7
|
||||
rotate right 4 steps
|
||||
reverse positions 3 through 6
|
||||
move position 0 to position 6
|
||||
swap position 3 with position 5
|
||||
swap letter e with letter h
|
||||
rotate based on position of letter c
|
||||
swap position 4 with position 7
|
||||
reverse positions 0 through 5
|
||||
rotate right 5 steps
|
||||
rotate left 0 steps
|
||||
rotate based on position of letter f
|
||||
swap letter e with letter b
|
||||
rotate right 2 steps
|
||||
rotate based on position of letter c
|
||||
swap letter a with letter e
|
||||
rotate left 4 steps
|
||||
rotate left 0 steps
|
||||
move position 6 to position 7
|
||||
rotate right 2 steps
|
||||
rotate left 6 steps
|
||||
rotate based on position of letter d
|
||||
swap letter a with letter b
|
||||
move position 5 to position 4
|
||||
reverse positions 0 through 7
|
||||
rotate left 3 steps
|
||||
rotate based on position of letter e
|
||||
rotate based on position of letter h
|
||||
swap position 4 with position 6
|
||||
reverse positions 4 through 5
|
||||
reverse positions 5 through 7
|
||||
rotate left 3 steps
|
||||
move position 7 to position 2
|
||||
move position 3 to position 4
|
||||
swap letter b with letter d
|
||||
reverse positions 3 through 4
|
||||
swap letter e with letter a
|
||||
rotate left 4 steps
|
||||
swap position 3 with position 4
|
||||
swap position 7 with position 5
|
||||
rotate right 1 step
|
||||
rotate based on position of letter g
|
||||
reverse positions 0 through 3
|
||||
swap letter g with letter b
|
||||
rotate based on position of letter b
|
||||
swap letter a with letter c
|
||||
swap position 0 with position 2
|
||||
reverse positions 1 through 3
|
||||
rotate left 7 steps
|
||||
swap letter f with letter a
|
||||
move position 5 to position 0
|
||||
reverse positions 1 through 5
|
||||
rotate based on position of letter d
|
||||
rotate based on position of letter c
|
||||
rotate left 2 steps
|
||||
swap letter b with letter a
|
||||
swap letter f with letter c
|
||||
swap letter h with letter f
|
||||
rotate based on position of letter b
|
||||
rotate left 3 steps
|
||||
swap letter b with letter h
|
||||
reverse positions 1 through 7
|
||||
rotate based on position of letter h
|
||||
swap position 1 with position 5
|
||||
rotate left 1 step
|
||||
rotate based on position of letter h
|
||||
reverse positions 0 through 1
|
||||
swap position 5 with position 7
|
||||
reverse positions 0 through 2
|
||||
reverse positions 1 through 3
|
||||
move position 1 to position 4
|
||||
reverse positions 1 through 3
|
||||
rotate left 1 step
|
||||
swap position 4 with position 1
|
||||
move position 1 to position 3
|
||||
rotate right 2 steps
|
||||
move position 0 to position 5
|
8
2016/day21-scrambled_letters_and_hash/tests/sample_input
Normal file
8
2016/day21-scrambled_letters_and_hash/tests/sample_input
Normal file
|
@ -0,0 +1,8 @@
|
|||
swap position 4 with position 0
|
||||
swap letter d with letter b
|
||||
reverse positions 0 through 4
|
||||
rotate left 1 step
|
||||
move position 1 to position 4
|
||||
move position 3 to position 0
|
||||
rotate based on position of letter b
|
||||
rotate based on position of letter d
|
Loading…
Add table
Add a link
Reference in a new issue