Part 1
Description
Little Jane Marie just got her very first computer for Christmas from some unknown benefactor. It comes with instructions and an example program, but the computer itself seems to be malfunctioning. She's curious what the program does, and would like you to help her run it.The manual explains that the computer supports two registers and six instructions (truly, it goes on to remind the reader, a state-of-the-art technology). The registers are named
a and b, can hold any non-negative integer, and begin with a value of 0. The instructions are as follows:hlf rsets registerrto half its current value, then continues with the next instruction.tpl rsets registerrto triple its current value, then continues with the next instruction.inc rincrements registerr, adding1to it, then continues with the next instruction.jmp offsetis a jump; it continues with the instructionoffsetaway relative to itself.jie r, offsetis likejmp, but only jumps if registerris even ("jump if even").jio r, offsetis likejmp, but only jumps if registerris1("jump if one", not odd).
+ or - to indicate the direction of the jump (forward or backward, respectively). For example, jmp +1 would simply continue with the next instruction, while jmp +0 would continuously jump back to itself forever.The program exits when it tries to run an instruction beyond the ones defined.
For example, this program sets
a to 2, because the jio instruction causes it to skip the tpl instruction:inc a
jio a, +2
tpl a
inc a
What is the value in register b when the program in your puzzle input is finished executing?Input
Solution
This problem involves a sequence known as look and say sequence. And I found a ready to use VBA code here. I modified a bit into this:Sub LookAndSay() Dim s, news, curdigit, newdigit As String Dim curlength, p, L As Long ActiveSheet.Range("a4").Select s = Selection.Value For i = 1 To 40 L = Len(s) p = 1 curdigit = Left$(s, 1) curlength = 1 news = "" For p = 2 To L newdigit = Mid$(s, p, 1) If curdigit = newdigit Then curlength = curlength + 1 Else news = news & CStr(curlength) & curdigit curdigit = newdigit curlength = 1 End If Next p news = news & CStr(curlength) & curdigit Debug.Print news s = news ActiveCell.Offset(1, 0).Select ActiveCell.Value = Len(s) Next i Exit Sub End Sub
I modified the code so it will accommodate larger number (I change Integer into Long) and read input from and write output into the sheet. Then all I need is running the code and wait for the result.
Part 2
Description
Neat, right? You might also enjoy hearing John Conway talking about this sequence (that's Conway of Conway's Game of Life fame).Now, starting again with the digits in your puzzle input, apply this process 50 times. What is the length of the new result?
0 komentar:
Post a Comment