Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
70
Task/Bulls-and-cows-Player/Phix/bulls-and-cows-player.phix
Normal file
70
Task/Bulls-and-cows-Player/Phix/bulls-and-cows-player.phix
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
constant line = " +---------+-----------------------------+-------+------+\n"
|
||||
constant digits = "123456789"
|
||||
|
||||
function mask(integer ch)
|
||||
return power(2,ch-'1')
|
||||
end function
|
||||
|
||||
function score(sequence guess, sequence goal)
|
||||
integer bits = 0, bulls = 0, cows = 0
|
||||
for i=1 to length(guess) do
|
||||
if guess[i]=goal[i] then
|
||||
bulls += 1
|
||||
else
|
||||
bits += mask(goal[i])
|
||||
end if
|
||||
end for
|
||||
for i=1 to length(guess) do
|
||||
cows += (and_bits(bits,mask(guess[i]))!=0)
|
||||
end for
|
||||
return {bulls, cows}
|
||||
end function
|
||||
|
||||
sequence list = {}
|
||||
|
||||
procedure pick(integer n, integer got, integer marker, sequence buf)
|
||||
integer bits = 1
|
||||
if got>=n then
|
||||
list = append(list,buf)
|
||||
else
|
||||
for i=0 to length(digits)-1 do
|
||||
if not and_bits(marker,bits) then
|
||||
buf[got+1] = i+'1'
|
||||
pick(n, got+1, or_bits(marker,bits), buf)
|
||||
end if
|
||||
bits *= 2
|
||||
end for
|
||||
end if
|
||||
end procedure
|
||||
|
||||
procedure filter_list(sequence guess, integer bulls, integer cows)
|
||||
integer l = length(list), idx = 0
|
||||
sequence bc = {bulls,cows}
|
||||
for i=1 to l do
|
||||
if score(guess,list[i])=bc then
|
||||
idx += 1
|
||||
list[idx] = list[i]
|
||||
end if
|
||||
end for
|
||||
list = list[1..idx]
|
||||
end procedure
|
||||
|
||||
procedure game(sequence tgt)
|
||||
integer n = length(tgt), attempt = 1, bulls = 0, cows
|
||||
sequence guess
|
||||
pick(n,0,0,repeat(0,n))
|
||||
while bulls<n do
|
||||
guess = list[rand(length(list))]
|
||||
{bulls, cows} = score(guess,tgt)
|
||||
printf(1," | Guess %-2d| %9s %-14s | %d | %d |\n",
|
||||
{attempt, guess, sprintf("(from %d)",length(list)), bulls, cows})
|
||||
filter_list(guess, bulls, cows)
|
||||
attempt += 1
|
||||
end while
|
||||
puts(1,line)
|
||||
end procedure
|
||||
|
||||
constant N = 4
|
||||
sequence secret = shuffle(digits)[1..N]
|
||||
printf(1,"%s | Secret | %9s | BULLS | COWS |\n%s", {line, secret, line})
|
||||
game(secret)
|
||||
45
Task/Bulls-and-cows-Player/Sidef/bulls-and-cows-player.sidef
Normal file
45
Task/Bulls-and-cows-Player/Sidef/bulls-and-cows-player.sidef
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
# Build a list of all possible solutions. The regular expression weeds
|
||||
# out numbers containing zeroes or repeated digits.
|
||||
var candidates = (1234..9876 -> grep {|n| !("#{n}" =~ /0 | (\d) .*? \1 /x) }.map{.digits});
|
||||
|
||||
# Repeatedly prompt for input until the user supplies a reasonable score.
|
||||
# The regex validates the user's input and then returns two numbers.
|
||||
func read_score(guess) {
|
||||
loop {
|
||||
"My guess: %s (from %d possibilities)\n" \
|
||||
-> printf(guess.join, candidates.len);
|
||||
|
||||
if (var m = (Sys.scanln("bulls cows: ") =~ /^\h*(\d)\h*(\d)\h*$/)) {
|
||||
var (bulls, cows) = m.cap.map{.to_i}...;
|
||||
bulls+cows <= 4 && return(bulls, cows);
|
||||
}
|
||||
|
||||
say "Please specify the number of bulls and the number of cows";
|
||||
}
|
||||
}
|
||||
|
||||
func score_correct(a, b, bulls, cows) {
|
||||
var (exact, loose) = (0, 0);
|
||||
|
||||
for i in ^4 {
|
||||
a[i] == b[i] ? ++exact
|
||||
: (a[i]~~b && ++loose)
|
||||
}
|
||||
|
||||
(bulls == exact) && (cows == loose)
|
||||
}
|
||||
|
||||
# Pick a number, display it, get the score, and discard candidates
|
||||
# that don't match the score:
|
||||
loop {
|
||||
var guess = candidates.pick;
|
||||
var (bulls, cows) = read_score(guess);
|
||||
candidates.grep!{|n| score_correct(n, guess, bulls, cows) }
|
||||
candidates.len > 1 || break
|
||||
}
|
||||
|
||||
# Print the secret number or the error message
|
||||
(
|
||||
candidates.len == 1 ? ("Your secret number is: %d" % candidates[0].join)
|
||||
: ("I think you made a mistake with your scoring")
|
||||
)->say
|
||||
Loading…
Add table
Add a link
Reference in a new issue