Advent of Code: Day 21 Solution

This year, I’m posting my solutions to the puzzles in the Advent of Code.

Click the link to view the puzzle and code, along with a link to the solution on Github.

And now, on with the solution:

Day 21, part 1

Simplified Problem:

 

Modify a string based on the instructions in your puzzle input. Valid instructions are:

  • 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.

Following the instructions in the puzzle input, what is the output of scrambling abcdefgh?

Day 21, Part 2

Using the same instructions, what was the input that produced fbgdceah?

Solution

For part 2, the inverse rotation code was taken from Reddit:

 

 

You can find the full solution on Github

Leave a Reply

Your email address will not be published. Required fields are marked *