Page 1 of 1

Valuation - piece of code won't work

Posted: Sun Nov 16, 2008 3:28 pm
by matthias00
Hi there,

to solve 'Valuation', I've written a small piece of C#-code.

Code: Select all

    
static int recalc(int dx) {
	return dx - 48;
}

[...]

while(cnt < arrText.Length) {
	if (false == Char.IsDigit(arrText[cnt])) { // if 'x' occurs, set it to zero and move left
			arrText[cnt] = '0'; // write ASCII-'0' as char ('48' in dec)
			cnt -= 2; // go two steps left
					
	}
	
	int x = recalc((int)arrText[cnt]); // casting from char to int delivers ASCII-value in dec. To get the digit itself, recalc() subtracts 48 from each value.
	sum += x;
	cnt++;

}
arrText is an array consisting of 256 char-elements read from a text-file. This data seems to be correct. The output is everytime '1295', but it won't work.
Can someome give me a hint?

Thank you.

Posted: Sun Nov 16, 2008 4:48 pm
by rmplpmpl
Yepp, your error is, you are subsituting an X by zero on your way parsing the cipher. Try to find another solution

Posted: Sun Nov 16, 2008 5:10 pm
by matthias00
But adding zero to the sum shouldn't be problematic?
//okay, got it.

Posted: Sun Nov 16, 2008 6:37 pm
by m!nus
that was my fault at first aswell. i ended up just doing it with counter variables and not changing the string because it's faster

Valuation not working

Posted: Tue Feb 23, 2010 9:12 pm
by nahnoe
Is this challenge broken? I can't understand why my solution won't work, I've tested it a few times with data that I manually checked and it works. It doesn't 'remove' the X's.. it just stores the last two numbers read then re-adds them if it hits an X:

Code: Select all

$x = "93752xxx....";

@s = split(//, $x);

$j1=0;
$j2=0;

for ( $i=0; $i<scalar @s; $i++ ) {
	if ($s[$i] == "x") {
		$c += $j1 + $j2;
	} else {
		$j2 = $j1;
		$j1 = $s[$i];
		$c += $s[$i];
	}
}

print $c;
I've checked 123x456 and got 26.
I've checked 123x456x789 and got 61.
I've checked 123xx456 and got 31.
I've checked 123xx456xxx789 and got 88.
I've checked 1234xx5x67x8xx and got 102.

All of which seem consistent with my understanding of the problem.

If someone can see something that I can't, or if this is too spoiler-ish please let me know.

Posted: Tue Feb 23, 2010 9:53 pm
by tails
You can try $x = "120" and see how the code works.

Posted: Tue Feb 23, 2010 9:58 pm
by nahnoe
tails wrote:You can try $x = "120" and see how the code works.
Haha! Can't believe I missed that!.. Cheers bud.