Hello

There is a game called number baseball game. One person holds secret
his/her own n-digit number. The other player guesses the number with
many trials of n-digit numbers.

Strike is when the same digit is in the same position between the
answer and the guess numbers. Ball is when the same digit is in a
different position between the answer and the guess numbers.

For example, when the answer is 123 and the guess is 923, it is 2
strike and no ball.

It becomes tricky when there are multiple of the same digits. For
example when the answer is 998 and the guess is 129, it's no strike
and 1 ball(it's counting the possible pair). When the answer is 912
and the guess is 998, it is 1 strike and no ball.

Here is my solution:

score=: strike, ball
ball=: findall - strike
strike=: +/@:=
findall=: +/@(<./"1@:(+/"1)@(~.@, =/ ,:))

assert 1 0 -: 1 2 3 score 9 8 3
assert 2 0 -: 1 2 3 score 1 9 3
assert 0 1 -: 1 2 3 score 9 8 1
assert 0 2 -: 1 2 3 score 3 9 1

assert 1 0 -: 9 8 7 score 9 9 1
assert 0 2 -: 9 9 1 score 1 2 9

I am satisfied with "strike" verb but not with "findall" verb. Could
you come up with a more elegant and simpler verb?
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to