Wednesday, November 25, 2026

Advent of Code 2016 - Day 21: Scrambled Letters and Hash

Part 1

Description

A password is scrambled by a list of operations: swap two positions, swap two letters, rotate left or right by some steps, rotate based on the position of a letter, reverse part of the password, or move one letter to another position. The input is the list of operations, one per line, and Part 1 asks what the password abcdefgh becomes. 

Solution

I pasted the 100 operations into A5:A104 and put the starting password in B4. One formula applies them one after another with REDUCE, and p is the password so far. For each operation it needs three things:

  • The kind of operation, from the first nine characters of the line, because they are different for every kind (swap posi, swap lett, reverse p, rotate le, rotate ri, move posi and rotate ba).
  • The numbers in the line, e. For each digit from 0 to 9 I look for its position in the line, keep the ones found, sort them, and read the characters. Positions are single digits, because the password has 8 letters.
  • The letters in the line, f. The letters follow the word "letter", which ends in r, so I look for r a, r b and so on, and take the character after it.

Then SWITCH picks the right operation:

<
B2: =LET(
 a, $A$5:$A$104,
 b, LEFT(a,9),
 c, REDUCE("abcdefgh",SEQUENCE(ROWS(a)),
  LAMBDA(p,q,
   LET(
    d, INDEX(a,q),
    e, --MID(d,SORT(FILTER(FIND(SEQUENCE(10,,0),d),ISNUMBER(FIND(SEQUENCE(10,,0),d)))),1),
    f, MID(d,SORT(FILTER(FIND("r "&CHAR(SEQUENCE(10,,97)),d)+2,ISNUMBER(FIND("r "&CHAR(SEQUENCE(10,,97)),d)+2))),1),
    g, MID(p,INDEX(e,1)+1,1),
    x, SWITCH(INDEX(b,q),
     "swap posi",SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(p,g,"_"),MID(p,INDEX(e,2)+1,1),g),"_",MID(p,INDEX(e,2)+1,1)),
     "swap lett",SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(p,INDEX(f,1),"_"),INDEX(f,2),INDEX(f,1)),"_",INDEX(f,2)),
     "reverse p",CONCAT(MID(p,1,INDEX(e,1)),TEXTJOIN("",1,MID(MID(p,INDEX(e,1)+1,INDEX(e,2)+1-INDEX(e,1)),{10,9,8,7,6,5,4,3,2,1},1)),MID(p,INDEX(e,2)+2,LEN(p)-INDEX(e,2)-1)),
     "rotate le",CONCAT(MID(p,1+INDEX(e,1),LEN(p)-INDEX(e,1)),MID(p,1,INDEX(e,1))),"rotate ri",CONCAT(MID(p,LEN(p)-INDEX(e,1)+1,INDEX(e,1)),MID(p,1,LEN(p)-INDEX(e,1))),
     "move posi",CONCAT(MID(SUBSTITUTE(p,g,""),1,INDEX(e,2)),g,MID(SUBSTITUTE(p,g,""),INDEX(e,2)+1,LEN(p))),
     "rotate ba",LET(a,FIND(INDEX(f,1),p),b,IF(a<5,CONCAT(MID(p,LEN(p)-a+1,a),MID(p,1,LEN(p)-a)),CONCAT(MID(p,LEN(p)+1-MOD(a+1,LEN(p)),MOD(a+1,LEN(p))),MID(p,1,LEN(p)-MOD(a+1,LEN(p))))),b)
    ),
    x
   )
  )
 ),
 c
)
  • Swaps. I swap two letters through a temporary _ character.
  • Reverse. I cut out the part to reverse and read its letters backwards, using the index list {10,9,…,1}. The 1 in TEXTJOIN ignores the empty results for positions past its end.
  • Rotations. A left rotation moves the first n letters to the end, and a right rotation moves the last n letters to the front.
  • Move. I take the letter out, and put it back after the first e2 letters that remain.
  • Rotate based on a letter. The rotation is right by 1 plus the position of the letter (counting from 0), plus 1 more if the position is 4 or more. With a = position + 1, that is a steps when a is below 5, and a + 1 steps otherwise (wrapping around the 8 letters).

For the puzzle's example, the password abcde with the example's eight operations becomes decab.

Part 2

Description

Now I know the scrambled password, fbgdceah (typed in D4), and I need the original password that the same operations turned into it. 

Solution

I use the same formula with three changes. First, the operations are applied in reverse order. A5:A104 is sorted by line number from the last line to the first, and the first column is kept:

a: TAKE(SORT(HSTACK($A$5:$A$104,SEQUENCE(ROWS($A$5:$A$104))),2,-1),,1)

Second, every operation is undone by its opposite:

  • Swaps and reverses undo themselves.
  • Rotating left is undone by rotating right, and the other way around. That is why the "rotate le" and "rotate ri" branches are swapped.
  • move position X to position Y is undone by moving the letter that is now at position Y back to position X:
"move posi",CONCAT(MID(SUBSTITUTE(p,MID(p,INDEX(e,2)+1,1),""),1,INDEX(e,1)),
                   MID(p,INDEX(e,2)+1,1),
                   MID(SUBSTITUTE(p,MID(p,INDEX(e,2)+1,1),""),INDEX(e,1)+1,LEN(p)))

Third, the hard one: undoing rotate based on position of letter X. How far the password was rotated depends on where the letter was before, but I can tell from where it is now. For 8 letters each position gives a different answer. For a letter now at position 0 to 7, the password must be rotated left by 1, 1, 6, 2, 7, 3, 0 and 4 (or right by 7, 7, 2, 6, 1, 5, 0 and 4) to undo it. The formula computes the right rotation b from the position a of the letter:

"rotate ba",LET(a,FIND(INDEX(f,1),p),
                b,MOD(8-a+IF(MOD(a,2)=0,MOD(a+6,8),IF(a=1,a+13,a+5))/2+1,8),
                CONCAT(MID(p,LEN(p)-b+1,b),MID(p,1,LEN(p)-b)))

Everything else is the same as in Part 1, with $D$4 as the starting password. In the end I scrambled the result again with Part 1's formula to check that I got fbgdceah back.

Share:

0 komentar:

Recent Posts

Categories

Blog Archive