Here are my solutions in C#
using System;
public class ComparerInator
{
public delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2);
public int makeProgram(int[] A, int[] B, int[] wanted)
{
if (test(A, B, wanted, f_a) || test(A, B, wanted, f_b))
return 1;
if (test(A, B, wanted, f_ab) || test(A, B, wanted, f_ba))
return 7;
return -1;
}
bool test(int[] A, int[] B, int[] wanted, Func<int, int, int> f)
{
for (int i=0; i < A.Length; i++)
{
if (f(A[i], B[i]) == wanted[i])
continue;
return false;
}
return true;
}
int f_a(int a, int b)
{
return a;
}
int f_b(int a, int b)
{
return b;
}
int f_ab(int a, int b)
{
return a<b?a:b;
}
int f_ba(int a, int b)
{
return a<b?b:a;
}
}using System;
using System.Text;
public class MathContest
{
public int countBlack(String ballSequence, int repetitions)
{
StringBuilder sb = new StringBuilder(ballSequence.Length * repetitions);
for (int i = 0; i < repetitions; i++)
sb.Append(ballSequence);
s = sb.ToString();
pos = -1;
invert = false;
direction = 1;
end = s.Length - 1;
int count = 0;
char ball = get_next();
while (ball != (Char)0)
{
if (ball == 'B')
count++;
ball = get_next();
}
return count;
}
string s;
int pos;
bool invert;
int direction;
int end;
char get_next()
{
if (pos == end)
return (Char)0;
pos += direction;
char ch = s[pos];
if (invert)
{
if (ch == 'B')
ch = 'W';
else
ch = 'B';
}
if (ch == 'B')
invert = invert ? false : true;
else
{
int t = end;
end = pos + direction;
pos = t + direction;
direction = direction == 1 ? -1 : 1;
}
return ch;
}
}
Unfortunately I've made a mistake in 500-point problem. Instead of
end = pos + direction;
pos = t + direction;
I wrote
end = pos;
pos = t;
so my solution was successfully challenged and gave me zero points.More then it - it was my first competition on TopCoder and there was a problem running the engine. So results are not go to rating. Well... next time.