
Solution to Event 4 in the 2008 Winter Scripting Games Sudden Death Challenge.
You know, had we wanted to, we could have made this event much harder. After all, we could have simply said, “OK, write a script that can turn 6882463283678273808479 into DR. SCRIPTO.” But the Scripting Guys are too kind-hearted; that’s why our instructions included this paragraph:
You say that’s not a good enough hint? Sorry, but that’s the only hint we can give you. No point in you even bothering to … asc. |
Hopefully, that clued you in to the fact that the key to solving this script was converting ASCII values to characters. If that didn’t clue you in and yet you still managed to solve this problem we have just one thing to say: how in the world did you manage to do that?
Incidentally, here’s how the Scripting Guys managed to do it. All we’ll show you today is a VBScript script; however, you could also easily solve the problem using Windows PowerShell or Perl:
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Scripts\Numbers.txt", ForReading)
strText = objFile.ReadAll
objFile.Close
intLength = Len(strText)
For i = 1 to intLength Step 2
strCharacters = Mid(strText, i, 2)
strNewString = strNewString & Chr(strCharacters)
Next
Wscript.Echo strNewString
To solve this problem our first step is to define a constant named ForReading; we’ll need that when we go to open our text file, C:\Scripts\Numbers.txt. Speaking of which, once we define the constant we create an instance of the Scripting.FileSystemObject object, then use this line of code to open Numbers.txt for reading:
Set objFile = objFSO.OpenTextFile("C:\Scripts\Numbers.txt", ForReading)
As soon as the file is open we use the ReadAll method to read in the contents of the file, storing that information in a variable named strText. That makes strText equal to this:
6882463283678273808479
Believe it or not, at that point we’re done with Numbers.txt; therefore, we use the Close method to close the file.
Our next step is to use VBScript’s Len function to determine the length (number of characters) in the string strText:
intLength = Len(strText)
Why do we do that? Well, we’re going to grab characters from the string value, 2 at a time. That’s relatively easy, except for one thing: we have to know when to stop grabbing characters. By determining the total number of characters in the string, we’ll know exactly when to stop.
That brings us to this line of code:
For i = 1 to intLength Step 2
What we’ve done here is set up a For loop that starts at 1 and continues on through the end of the string. Notice, too that we added the parameter Step 2. What’s that for? The Step 2 parameter tells the For loop that we want to increment the counter variable i by 2 each time we go through the loop. That means that, the first time through the loop, i will be equal to 1. In turn that means we’ll start at character 1 and (as you’ll soon see) we’ll grab two characters: characters 1 and 2. The next time through the loop i will be equal to 3, and we’ll grab characters 3 and 4. Needless to say, that’s how we manage to grab our character pairs.
Speaking of grabbing character pairs, that’s what we do with this line of code:
strCharacters = Mid(strText, i, 2)
As you can see, here we’re simply using the Mid function the grab two characters from the string, beginning with character i; in our case, that’s going to be these two characters: 68. And what do we do with those two characters? This:
strNewString = strNewString & Chr(strCharacters)
We’re actually doing two things in this line of code. First, we’re using the Chr function to determine the keyboard character corresponding to the ASCII value 68. As it turns out, 68 is the ASCII code for the letter D. That means that the first number pair in our string is really the letter D. We take that value, append it to the variable strNewString, then go back to the top of the loop and repeat the process with the next number pair.
When we’ve finished with all the number pairs in the string we echo back the final results:
DR. SCRIPTO
Nice, huh? The table below shows you the correspondence between each number pair and each letter in the string DR. SCRIPTO:
D | R | . | S | C | R | I | P | T | O | |
68 | 82 | 46 | 32 | 83 | 67 | 82 | 73 | 80 | 84 | 79 |