루비, 139 바이트
Lambda 함수, x와 y를 인수로 사용하고 문자열을 반환
->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]}
솔루션이 존재하면 모든 코알라 + commapillars 또는 모든 코알라 + crabs로 수행 할 수 있습니다.
원칙은 최소한의 commapillars를 사용하는 것입니다. 숫자가 홀수이면 1 개의 commapillar을 사용합니다. 마침표보다 쉼표가 더 많지 않으면 0 쉼표를 사용하더라도 2를 사용합니다.
noncommapillars (crabs + koalas)에서 사용되는 기간은 반드시 짝수이고 noncommapillars의 수는 절반 (number of periods)-(number of commapillars)
입니다. 모든 코알라에 대해 쉼표가 충분하지 않거나 모든 게에 대해 너무 많으면 해결책이 없습니다. 그렇지 않으면 솔루션을 반환합니다.
테스트 프로그램에서 언급
명확성을 위해 카멜레온 대신 "실패"를 사용합니다.
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
산출
, 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
.........,,,,,,, <.,.> <.,.> <.,.> <.,.> <,,,.>
.........,,,,,,,, <.,.> <.,.> <.,.> <.,.> <,,,,.>
.........,,,,,,,,, <.,.> <.,.> <.,.> <.,.> <,,,,,.>