Wednesday, November 18, 2026

Advent of Code 2016 - Day 8: Two-Factor Authentication

Part 1

Description

A small screen, 50 pixels wide and 6 pixels tall, starts with every pixel off. A list of instructions changes it:

  • rect AxB turns on all the pixels in the top-left corner, A wide and B tall.
  • rotate row y=A by B shifts row A to the right by B pixels, and pixels that fall off the end come back at the start.
  • rotate column x=A by B does the same for column A, shifting down.

Part 1 asks how many pixels are lit after all the instructions. 

Solution

I pasted the 162 instructions into A5:A166. One formula builds the whole screen. It starts with a grid of zeros (6 rows, 50 columns) and uses REDUCE to apply the instructions one after another. At each step p is the screen so far and q is the next instruction:

I5: =LET(a,SEQUENCE(6),
         b,SEQUENCE(,50),
         c,REDUCE(a*b*0,A5:A166,
           LAMBDA(p,q,
             LET(d,TOROW(--(TEXTSPLIT(q,{" ","x","=","by"})),3),
                 xx,INDEX(d,1,1),
                 yy,INDEX(d,1,2),
                 SWITCH(MID(q,8,1),
                   "c",IF(b=xx+1,VSTACK(TAKE(p,-yy),DROP(p,-yy)),p),
                   "r",IF(a=xx+1,HSTACK(TAKE(p,,-yy),DROP(p,,-yy)),p),
                   IF((b<=xx)*(a<=yy),1,p))))),
         c)
  • a is the row numbers 1 to 6 and b is the column numbers 1 to 50.
  • d cuts the instruction into words and numbers, and keeps only the two numbers (the words give errors, which TOROW(…,3) skips). xx is the first number and yy is the second.
  • The eighth character of an instruction tells its type: “rotate row” has an r there, “rotate column” has a c, and rect has a digit or an x.
  • For a column rotation, VSTACK(TAKE(p,-yy),DROP(p,-yy)) is the screen with its last yy rows moved to the top. The IF(b=xx+1,…,p) uses that only for the chosen column.
  • For a row rotation, HSTACK(TAKE(p,,-yy),DROP(p,,-yy)) moves the last yy columns to the front, and the IF(a=xx+1,…,p) uses it only for the chosen row.
  • For a rect, IF((b<=xx)*(a<=yy),1,p) turns on the top-left xx by yy block.

The formula fills I5:BF10, a block of 1s and 0s. The answer is the number of 1s:

B2: =SUM(I5#)

Part 2

Description

The screen is supposed to show a code made of letters, and Part 2 asks for it.

Solution

No new calculation is needed, because the screen is already there. I added conditional formatting (colour the cells whose value is 1) to I5:BF10 so the lit pixels stand out, and the letters can be read by eye. I typed what I read into D2.

Another way: a macro. As a cross-check I also drew the screen with a macro. In B5, =MID(A5,8,1) gives the instruction type (r, c or anything else for rect) and C5:D5 hold the two numbers. The macro goes through them and draws the result in I18:BF23:

Public Sub DrawScreen()
    Dim ws As Worksheet: Set ws = Worksheets("2016")
    Dim out As Range: Set out = ws.Range("I18:BF23")      ' 6 rows x 50 columns
    Dim keep(1 To 50) As Variant
    Dim r As Long, i As Long, j As Long, k As Long
    Dim ins As String, a As Long, b As Long

    out.Value = 0
    r = 5
    Do Until ws.Cells(r, 2).Value = ""
        ins = ws.Cells(r, 2).Value
        a = ws.Cells(r, 3).Value
        b = ws.Cells(r, 4).Value
        Select Case ins
            Case "r"                                      ' rotate row a by b
                For i = 1 To 50: keep(i) = out(a + 1, i).Value: Next i
                For i = 1 To 50
                    k = (i - 1 + b) Mod 50 + 1
                    out(a + 1, k).Value = keep(i)
                Next i
            Case "c"                                      ' rotate column a by b
                For i = 1 To 6: keep(i) = out(i, a + 1).Value: Next i
                For i = 1 To 6
                    k = (i - 1 + b) Mod 6 + 1
                    out(k, a + 1).Value = keep(i)
                Next i
            Case Else                                     ' rect a x b
                For i = 1 To a
                    For j = 1 To b
                        out(j, i).Value = 1
                    Next j
                Next i
        End Select
        r = r + 1
    Loop
End Sub

Both ways give exactly the same picture.




 

Share:

0 komentar:

Recent Posts

Categories

Blog Archive