Part 1
Description
Today, the Elves are playing a game called look-and-say. They take turns making sequences by reading aloud the previous sequence and using that reading as the next sequence. For example,211 is read as "one two, two ones", which becomes 1221 (1 2, 2 1s).Look-and-say sequences are generated iteratively, using the previous value as input for the next step. For each step, take the previous value, and replace each run of digits (like
111) with the number of digits (3) followed by the digit itself (1).For example:
1becomes11(1copy of digit1).11becomes21(2copies of digit1).21becomes1211(one2followed by one1).1211becomes111221(one1, one2, and two1s).111221becomes312211(three1s, two2s, and one1).
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