BLOG

Viewing by Entry / Main
29 May, 2009
Code War 2009 Round 1 Question 3 Answer

So as I said, this problem is related to the ASCII decoding problem from the final last year. The key to solving the problem is coming up with the correct interpretation of “Checkerboard”. In the competition we had to give a few hints when the teams headed off with a 2D interpretation, but it's much simpler than that: all we've done is reverse the bits of every second character. I meant to use another language for this sample answer but I was having fun with AS3, so here's another one in ol' faithful:

package {
import flash.display.Sprite;
import flash.text.TextField;

public class CheckerboardMatrix extends Sprite {
	
	public function CheckerboardMatrix() {
		var source : String = 
			"010101001001011101100101110111110100110110011110011101001000110101101001" +
			"100001110010000010001000011000011000110000100000100111100010000010011000" +
			"011100101001101001100001100010110010000010010010011011111000100101101001" +
			"100110100010111011011111001000001010101101101111100100000010000010011101" +
			"011000011001101100100000100010110110100010011010011110011101111101101110" +
			"100110100111011010011010011100101101111101101101100111100110010010011010" +
			"001000001001111001101110100001100010000010001100011001011000111001110101" +
			"10011010011011001000110000101110";
			
		var c : Number;
		var out : Array = new Array();
		var f : TextField = new TextField();

		for (var i : Number = 0; i <= source.length - 1; i++) {
			if (i % 8 == 0) 
				c = 0;
			if (i % 16 < 8)
				c += ((source.charAt(i) == "1") ? 1 : 0) * Math.pow(2, 7 - (i % 8));
			else
				c += ((source.charAt(i) == "1") ? 0 : 1) * Math.pow(2, 7 - (i % 8));
			if (i % 8 == 7)
				out.push(String.fromCharCode(c));
		}
		
		f.text = 'The answer is "' + out.join('') + '"';
		f.width = 800;
		addChild(f);
	}
}
}

The decoded string is 'The Matrix was a great movie. Too bad they never made any sequels.' — a tip of the hat to our favourite online comic :-). Watch out for the final question from round one tomorrow.

Cheers, Robin

I used some nasty CF lol <cfset binstring = "01010100100101110110010111011111010011011001111001110100100011010110100110000111001000001000100001100001100011000010000010011110001000001001100001110010100110100110000110001011001000001001001001101111100010010110100110011010001011101101111100100000101010110110111110010000001000001001110101100001100110110010000010001011011010001001101001111001110111110110111010011010011101101001101001110010110111110110110110011110011001001001101000100000100111100110111010000110001000001000110001100101100011100111010110011010011011001000110000101110" /> <cfset newstring = ""> <cfset counter = 1> <cfloop from="1" to="#len(binstring)#" step="1" index="intChar"> <cfset strChar = Mid( binstring, intChar, 1 ) /> <cfif counter GT 8> <cfif strChar eq 1> <cfset strChar = 0> <cfelseif strChar eq 0> <cfset strChar = 1> </cfif> </cfif> <cfset newstring = "#newstring##strChar#"> <cfif counter GTE 16> <cfset counter = 1> <cfelse> <cfset counter = counter + 1> </cfif> </cfloop> <cfoutput>#newstring#</cfoutput>
Comment made by CF / Posted at Saturday 30 May, 2009 02:05

Post Your Comments