Ruby, 139 byte
Hàm Lambda, lấy x và y làm đối số và trả về một chuỗi
->x,y{c=y-2*n=y-(x>y ?1:0)>>1
x+=-c/2*s=[x-n,c*3].max
x<n||x>n*2?'~<.,,>~~':',<..>, '*(x-n)+'<.,.> '*(2*n-x)+"<%s.> "*c%[?,*s-=c/2*3,?,*3]}
Nếu một giải pháp tồn tại, nó có thể được thực hiện với tất cả gấu túi + commapillar hoặc tất cả gấu túi + cua.
Nguyên tắc là sử dụng tối thiểu các commapillar. Nếu số là số lẻ, chúng tôi sử dụng 1 commapillar. nếu thậm chí chúng ta sử dụng 0 dấu phẩy, trừ khi có nhiều dấu phẩy hơn dấu chấm, trong trường hợp đó chúng ta sử dụng 2 dấu phẩy.
Số lượng thời gian được sử dụng trong noncommapillar (cua + koalas) là nhất thiết phải chẵn, và số lượng noncommapillar là một nửa (number of periods)-(number of commapillars)
. Nếu không có đủ dấu phẩy cho tất cả gấu túi, hoặc quá nhiều cho tất cả cua, không có giải pháp nào khả thi. Nếu không, chúng tôi trả lại một giải pháp.
Nhận xét trong chương trình thử nghiệm
sử dụng "fail" thay vì tắc kè hoa cho rõ ràng
f=->x,y{c=y-2*n=y-(x>y ?1:0)>>1
#n=noncommapillars=y>>1 as they have 2 periods. c=commapillars=y-2*n, 1 for odd y, 0 for even y.
#if x>y there are too many commas to have 0 commapillars for even y. noncommapillars= y-1 >> 1, so 2 commapillars
x+=-c/2*s=[x-n,c*3].max
# s=number of commas allocated to commapillars. x-n to allow all noncommapillars to be koalas, but at least 3 per commapillar.
#-c/2 == -1 if there are commapillars, 0 if not (Ruby truncates toward -inf). Subtract commas for commapillars from x if necessary
x<n||x>n*2?'fail':',<..>, '*(x-n)+'<.,.> '*(2*n-x)+"<%s.> "*c%[?,*s-=c/2*3,?,*3]}
#if x<n (insufficient commas for all koalas) or x>n*2 (too many for all crabs) return fail. Else...
#return string off crabs, koalas, and (using % operator like sprintf) c commapillars (0..2), the second with 3 commas (if present) and the first with the rest.
10.times{|j|10.times{|i|puts "%-20s %s"%[?.*j+?,*i,f[i,j]]}}
#all x,y from 0..9
Đầu ra
, fail
,, fail
,,, fail
,,,, fail
,,,,, fail
,,,,,, fail
,,,,,,, fail
,,,,,,,, fail
,,,,,,,,, fail
. fail
., fail
.,, fail
.,,, <,,,.>
.,,,, <,,,,.>
.,,,,, <,,,,,.>
.,,,,,, <,,,,,,.>
.,,,,,,, <,,,,,,,.>
.,,,,,,,, <,,,,,,,,.>
.,,,,,,,,, <,,,,,,,,,.>
.. fail
.., <.,.>
..,, ,<..>,
..,,, fail
..,,,, fail
..,,,,, fail
..,,,,,, <,,,.> <,,,.>
..,,,,,,, <,,,,.> <,,,.>
..,,,,,,,, <,,,,,.> <,,,.>
..,,,,,,,,, <,,,,,,.> <,,,.>
... fail
..., fail
...,, fail
...,,, fail
...,,,, <.,.> <,,,.>
...,,,,, <.,.> <,,,,.>
...,,,,,, <.,.> <,,,,,.>
...,,,,,,, <.,.> <,,,,,,.>
...,,,,,,,, <.,.> <,,,,,,,.>
...,,,,,,,,, <.,.> <,,,,,,,,.>
.... fail
...., fail
....,, <.,.> <.,.>
....,,, ,<..>, <.,.>
....,,,, ,<..>, ,<..>,
....,,,,, fail
....,,,,,, fail
....,,,,,,, <.,.> <,,,.> <,,,.>
....,,,,,,,, <.,.> <,,,,.> <,,,.>
....,,,,,,,,, <.,.> <,,,,,.> <,,,.>
..... fail
....., fail
.....,, fail
.....,,, fail
.....,,,, fail
.....,,,,, <.,.> <.,.> <,,,.>
.....,,,,,, <.,.> <.,.> <,,,,.>
.....,,,,,,, <.,.> <.,.> <,,,,,.>
.....,,,,,,,, <.,.> <.,.> <,,,,,,.>
.....,,,,,,,,, <.,.> <.,.> <,,,,,,,.>
...... fail
......, fail
......,, fail
......,,, <.,.> <.,.> <.,.>
......,,,, ,<..>, <.,.> <.,.>
......,,,,, ,<..>, ,<..>, <.,.>
......,,,,,, ,<..>, ,<..>, ,<..>,
......,,,,,,, fail
......,,,,,,,, <.,.> <.,.> <,,,.> <,,,.>
......,,,,,,,,, <.,.> <.,.> <,,,,.> <,,,.>
....... fail
......., fail
.......,, fail
.......,,, fail
.......,,,, fail
.......,,,,, fail
.......,,,,,, <.,.> <.,.> <.,.> <,,,.>
.......,,,,,,, <.,.> <.,.> <.,.> <,,,,.>
.......,,,,,,,, <.,.> <.,.> <.,.> <,,,,,.>
.......,,,,,,,,, <.,.> <.,.> <.,.> <,,,,,,.>
........ fail
........, fail
........,, fail
........,,, fail
........,,,, <.,.> <.,.> <.,.> <.,.>
........,,,,, ,<..>, <.,.> <.,.> <.,.>
........,,,,,, ,<..>, ,<..>, <.,.> <.,.>
........,,,,,,, ,<..>, ,<..>, ,<..>, <.,.>
........,,,,,,,, ,<..>, ,<..>, ,<..>, ,<..>,
........,,,,,,,,, <.,.> <.,.> <.,.> <,,,.> <,,,.>
......... fail
........., fail
.........,, fail
.........,,, fail
.........,,,, fail
.........,,,,, fail
.........,,,,,, fail
.........,,,,,,, <.,.> <.,.> <.,.> <.,.> <,,,.>
.........,,,,,,,, <.,.> <.,.> <.,.> <.,.> <,,,,.>
.........,,,,,,,,, <.,.> <.,.> <.,.> <.,.> <,,,,,.>