Julia 0.6, 71 61 59 57 bytes
A->((r,s)=sort(A);r<3?s^~-r:3r+(s^2-4s+3)*((r==s)+r-2)-3)
Try it online!
Ungolfed (well, a bit more readable):
function f_(A)
(r, s) = sort(A)
if r < 3
result = s^(r-1)
else
result = 3*r +
(s^2 - 4*s + 3) * ((r == s) + r - 2) -
3
end
return result
end
What does it do?
Takes input as array A
containing r and s. Unpacks the array into r and s with the smaller number as r, using (r,s)=sort(A)
.
If r is 1, output should be 1. If r is 2, output should be whatever s is.
sr−1 will be s0=1 for r=1, and s1=s for r = 2.
So, r<3?s^(r-1)
or shorter, r<3?s^~-r
For the others, I started with noticing that the output is:
- for r = 3, 2×3+[0,3,8] (for s = 3, 4, 5 respectively).
- for r = 4, 2×4+ [10,17] (for s = 4, 5 respectively)
- for r = 5, 2×5+ [35] (for s = 5)
(I initially worked with f(5,5)=45 for convenience.)
This looked like a potentially usable pattern - they all have 2r
in common, 17 is 8*2+1, 35 is 17*2+1, 10 is 3*3+1. I started with extracting the base value from [0, 3, 8], as [0 3 8][s-2]
(this later became the shorter (s^2-4s+3)
).
Attempting to get correct values for r = 3, 4, and 5 with this went through many stages, including
2r+[0 3 8][s-2]*(r>3?3-s+r:1)+(r-3)^3+(r>4?1:0)
and
2r+(v=[0 3 8][s-2])+(r-3)*(v+1)+(r==s)v
Expanding the latter and simplifying it led to the posted code.
5,5
)kolmogorov-complexityの下に収まるかもしれないと思います(または、非入力、固定出力のみに収まりますか?)