Python 3, 371371 274 bytes
def f(s,n):
for y in range(a:=range(n)):
for x in range(n)a:
r=min(x,y,n-x-1n+~x,n-y-1n+~y);i=4*r*(n-r)-2*r+x+y+2*(2*(n-r-1-xn+~r)+(n-r-1x-y)*(y+x<n-1))*(y>x);H=;H=max(x<n-n+~y,y-1or x<y)and>x>min(x>yy-2or x>n2,n-y-2)
if i<len(s):c=s[i]
elif x+1-(n%2)==y==n/;b=x+1>n/2:c="<>"[n%2]
elif2;D=(n-2n+~x-2*xx)**b+x==n+~y;print(x+1>n/2)+x==y-1:c="."
s[i]if elifi<len(n-1-2*xs)*else".<.>"[D+n%2*2]if(x+1>n/2n+2*~x)+x==n-y*b+x==y-1:c="'"
else:c="|1else"|'-"[H]
print(c'"[D+2*H],end=" -"[H])
print()
Try it online!Try it online!
Minus 64 bytes thanks to @noodleman and another 11 bytes thanks to @Kevin Cruijssen! I then saved another 22 bytes.
Instead of most other solutions (as far as I can tell), I tried calculating the index into the string. I dug up a post on StackOverflow that helped with this, though a lot was just throwing random equations and seeing what they do. I'm rather certain Maybe the equationsindexing equation can be golfed further if I'd actually think about what I'm doingsimplified with a bit more thought, but it's surprisingly short already.
The obvious next step is to replaceThere are lots of places where - x - 1
is replaced with +~x
, (or variants). Booleans are heavily used for indexing and the similar trickscancelling parts of equations with inversemultiplication.
def function(string: str, number: int):
for y in range(number):
for x in range(number):
is_up_cornerrounds = min(x, +y, (number - 2 - 2 * x) * (x +- 1 >, number / 2) ==- y - 1)
is_down_corner = x + (number - 1 - 2 * x) * (x + 1 ># numbertotal /increments 2)after ==`rounds` numberrevolutions -and yfix -for 1overcounting
is_middleindex = x4 +* 1rounds -* (number %- 2rounds) == y ==- number2 //* 2rounds
# is_left_cornerindex =+= yx + (number - 2 * y) * (y > number / 2) ==# xeast + 1south increment
#index is_right_corner+= =2 y* +(2 * (number - rounds - 1) - 2x *- y) * (y > number / 2x) != number -# xwest -+ 1north
roundsis_horizontal = minmax(x, y, number - xy -+ 1, number - y -+ 12)
# sum> ofx increments+ per2 round> andmin(y, fixnumber for- overcountingy)
indexis_right = 4x *+ rounds1 *> (number - rounds) -/ 2 * rounds
indexis_up_corner +== x # east increment
index += y # south increment
index +=+ 2is_right * (number - rounds2 -* 1(x -+ x1)) *== (y >- x)1 # westinlined
indexis_down_corner +== 2x + is_right * (number - rounds - 1 - y)2 * (y + x) <== number - 1) * (y > x) #- north1
is_horizontal# is_middle = ((x < y or+ x1 <- number -% y2 -== 1)
y == number // 2
# is_middle = is_up_corner and is_down_corner
# is_down and= (xy > ynumber -/ 2
or x > number - y# -is_left_corner 2))
= y + is_down * (number if- index2 <* len(stringy):
== x + 1
character = string[index]
# is_right_corner = y + is_down elif* is_middle:
(number - 1 - 2 * y) != characternumber =- "<>"[numberx %- 2]1
elif is_up_corner:print(
character = "."
string[index] if index < eliflen(string) is_down_corner:else
character".<.>"[is_down_corner =+ "'"
2 * (number % 2)] if elifis_up_corner is_horizontal:else
character = ""|'-"
'"[is_down_corner + 2 * else:is_horizontal],
character =end=" "|"-"[is_horizontal]
print(character, end='-' if is_horizontal else ' ')
print()
Explanation of is_horizontal
boolean:
There are effectively four quadrants in which we need to determine whether the coordinate is a horizontal part in the snake, we can do this for the upper right corner with number - y - 1 > x
. On its own this is not enough, because the lower part of the snake is now messed up. Taking y > x
works for the lower right quadrant, but messes up the snake above. Combining these two equations into max(number - y - 1, y) > x
leaves only the left part of the snake to fix. Creating conditions for the other corners you can combine x > min(y - 2, number - y - 2)
into one large expression.
------------. ------------. ------------.
.---------. | .---------. | .---------. |
--.-----. | | --.-----. | | | .-----. | |
----.-> | | | becomes ----.-> | | | becomes | | .-> | | |
----' | ' | | ----'---' | | | | '---' | |
--' | | | ' | --'-------' | | '-------' |
' | | | | | ' '-----------' '-----------'