Typo

Discussion of challenges you have already solved
Post Reply
theStack
Posts: 72
Joined: Sun Nov 02, 2008 12:46 am

Typo

Post by theStack »

How did you solve that challenge?
I first tried around a bit with changing arithmetic operators but unfortuntely, nothing worked. I was convinced that it must be some PHP-specific stuff but I'm not a great php fan so I decided to brute-force that stuff with gave me the opportunity to get into a script language I wanted to learn since a long time anyway - Python. I solved most of the programming challenges before with C (sometimes Java), which can get quite annoying when it comes to string manipulation etc.

So here it is, my first python script which gave me the solution for this challenge:

Code: Select all

#!/usr/bin/env python
import os

# php statement we want to brute-force
statement_pre =  '$y = 79; '
statement =      '$x = $y * 2245 + ($y * 2 - 7);'
statement_post = ' echo $x;'

for position in range(0, len(statement)):
	for token in range(0x20, 0x7f):
		out_str =  statement_pre
		out_str += statement[0:position] + chr(token) + statement[position+1:]
		out_str += statement_post
		out_str = out_str.replace('$', '\$')
		if os.popen('php -r "' + out_str + '"').readline().find('8841') != -1:
			print 'Solution found!'
			print 'position: ' + str(position) + ', character: ' + chr(token)
			exit(1);
Well, I have to say I like Python so far.
The forced indenting is maybe a bit unfamailiar at first and the fact that all strings are immutable shocked me (no somestring[bla] = 'x' allowed), but I think I'm getting used to and after seeing some source code from others it seems that the readability of Python scripts is really good - compared e.g. to the Perl nightmare ;)

Anyway, the solution wasn't as hard as I thought and in fact it would have been easy to find out without scripting, but it was a nice exercise :D
User avatar
m!nus
Posts: 202
Joined: Sat Jul 28, 2007 6:49 pm
Location: Germany

Post by m!nus »

yep, wasn't too hard, I found after trying for about 10 minutes
dotme
Posts: 10
Joined: Sun Nov 16, 2008 6:45 pm

Post by dotme »

I just filled in the values and reformed the equation

Code: Select all

8841 = 79 * 2245 + (79 * 2 - 7);
8841 = 79 * 2245 + 151;           // - 151
8690 = 79 * 2245                  // / 79
110  = 2245
Than the solution was obvious how to modify "2245" to solve this equation.
It was likely that the first term "79 * 2245" has to be modified, because I couldn't imagine a solution for "177355 + (79 * 2 - 7)".
megabreit
Posts: 141
Joined: Sat Jan 03, 2009 3:33 pm

Post by megabreit »

Just thinking a few minutes how to solve that with brute force and evaluating
all the strings. I decided it's not worth it, took a pen and a sheet of paper
and solved it in a few minutes.

BTW: perl is no nightmare... it's the programmer who makes things cryptic.
You were looking to the wrong scripts (and maybe to early to understand them).
It's easy to write cryptic code in any language... see some of the Java challenges like Branches!
Don't judge just by looking at the code! And if we speak about other programming languages:
I started to like ruby while doing some challenges. :twisted:
theStack
Posts: 72
Joined: Sun Nov 02, 2008 12:46 am

Post by theStack »

megabreit wrote:BTW: perl is no nightmare... it's the programmer who makes things cryptic.
You were looking to the wrong scripts (and maybe to early to understand them).
It's easy to write cryptic code in any language... see some of the Java challenges like Branches!
Don't judge just by looking at the code! And if we speak about other programming languages:
I started to like ruby while doing some challenges. :twisted:
Of course you _can_ make every piece of code look cryptic in any programming language, no doubt about that, but why should anybody want to do that (exception: special contests about cryptic code pieces)? Let's emanate from an average programmer who isn't completely mad and knows what he's doing. I think it's quite obvious that, even like already said he is willing to write readable code, the probability to write a readable program in language that _is especially_ designed to be readble is much higher than in another language where dozens of dirty tricks are allowed and you always have to carefully watch that your code doesn't get messy.
I don't say it's impossible to write unreadable code in Python - readability also much depends on meaningful variable and function naming and so on - but on average it's much harder than e.g. in perl.

See for example famous Eric Raymonds opinion on this:
http://pythonology.org/success&story=esr
AMindForeverVoyaging
Forum Admin
Posts: 497
Joined: Sat May 28, 2011 9:14 am
Location: Germany

Post by AMindForeverVoyaging »

I agree that brute force is probably rather overkill here. The way the formula is set up, there are not really that many places where you could make the change. The multiplication with 2245 makes the result way too big, so you gotta correct that... I just made a very little PHP script, just the formula itself and printing out the result, and played with it for like ten minutes until I found the solution. 'Another Typo' actually went faster for me, I think I accidentally found the solution for that one first but could not remember at once which character I had changed for that one :D
Post Reply