This blog no longer exists.
You will be redirected to something cool.

Monday, December 24, 2012

Secret Code Programming Puzzle

I'm trying to solve a mathy programming problem. (I came up with the problem a couple of years ago, but it's still preying on my mind.)

There is a secret code (which you're allowed unlimited guesses at) => a string 12 characters in length. Each character can be either a number or a letter (a-z, 0-9) and it's not case-sensitive (A == a).

After every hour, a character in the secret code is revealed.

For example:
Hour 1:
_ _ Q _ _ _ _ _ _ _ _ _

Hour 2:
_ _ Q _ _ _ _ _ _ _ 4 _

Hour 3:
_ _ Q _ _ _ _ 7 _ _ 4 _

Hour 12:
1 C Q 8 2 V V 7 J B 4 9

I want to write code that solves the code before a human can. I figure a human can realistically start manually entering combinations at around hour 10. I would like my program to solve it before then.

What should the program do when another hour goes by and another secret character is revealed? I was thinking of starting out with an array of all the possible combinations, but populating and storing the array would take an immense amount of time and resources. I was thinking about storing this array and when a new character is revealed, rejecting ALL strings from the array that don't have the new secret character in the exact position.

At a worse case scenario of making guesses with all 12 characters left unrevealed, we're left with awful time complexity:
36 numbers and letters, 12 mystery characters
36^12 == somewhere considerably longer than I have remaining in my lifetime given however long it takes in a single-threaded program to make a guess. Still too long in a multi-threaded program on the world's best computer.

However, this goes down significantly after each hour. So I have several questions to answer:

Should I start my program at a certain hour? If so, would there be any benefit to at least STARTING to populate an array before said starting hour? Would I even need to populate a massive array?

Monday, December 17, 2012

Best Practices in Comparison

I have a best practices question about some code.

Which code is easier for another programmer to understand:

Should I switch the greater_than and less_than signs [as in elsif in the first example (lines 1-5)] or should I switch the position of rocks and pebbles [as in the elsif in the second example(lines 7-11).] I prefer the second example because the puts statement is essentially the same as its if statement.

However, maybe it's easier for some people to think of the comparison in terms of, well, in this case, rocks. (I really don't know why.) What do you think?