Chore: checkpoint
This commit is contained in:
135
snake.py
Normal file
135
snake.py
Normal file
@@ -0,0 +1,135 @@
|
||||
import solve, utils
|
||||
|
||||
def grow():
|
||||
N = get_world_size()
|
||||
apple = None
|
||||
path = 0
|
||||
length = 0
|
||||
|
||||
def mv(x, y, d, irq=False):
|
||||
global apple
|
||||
global path
|
||||
global length
|
||||
|
||||
move(d)
|
||||
path += 1
|
||||
if (x, y) == apple:
|
||||
apple = measure()
|
||||
length += 1
|
||||
return irq and path >= length
|
||||
|
||||
def goto(tx, ty, irq=False):
|
||||
if irq and path >= length:
|
||||
return
|
||||
|
||||
x, y = get_pos_x(), get_pos_y()
|
||||
|
||||
for i in range(max(0, tx-x)):
|
||||
x += 1
|
||||
if mv(x, y, East, irq):
|
||||
return
|
||||
for i in range(max(0, x-tx)):
|
||||
x -= 1
|
||||
if mv(x, y, West, irq):
|
||||
return
|
||||
|
||||
for i in range(max(0, ty-y)):
|
||||
y += 1
|
||||
if mv(x, y, North, irq):
|
||||
return
|
||||
for i in range(max(0, y-ty)):
|
||||
y -= 1
|
||||
if mv(x, y, South, irq):
|
||||
return
|
||||
|
||||
|
||||
def init():
|
||||
global apple
|
||||
global length
|
||||
|
||||
change_hat(Hats.Gray_Hat)
|
||||
utils.mv(0, 0)
|
||||
change_hat(Hats.Dinosaur_Hat)
|
||||
|
||||
apple = measure()
|
||||
path = 0
|
||||
length = 1
|
||||
|
||||
def run():
|
||||
global apple
|
||||
global path
|
||||
|
||||
if apple == None or length == N*N:
|
||||
init()
|
||||
return
|
||||
|
||||
path = 0
|
||||
for x in range(N):
|
||||
if x%2 == 0:
|
||||
goto(x, 1, True)
|
||||
goto(x, N-1, True)
|
||||
else:
|
||||
goto(x, N-1, True)
|
||||
goto(x, 1, True)
|
||||
|
||||
x, y = apple
|
||||
if within((x, y), (0, N-1, 1, N)):
|
||||
goto(x, y)
|
||||
|
||||
goto(N-1, 1)
|
||||
goto(N-1, 0)
|
||||
goto(0, 0)
|
||||
|
||||
x, y = apple
|
||||
|
||||
return (init, run)
|
||||
|
||||
def growlong():
|
||||
N = get_world_size()
|
||||
|
||||
def init():
|
||||
change_hat(Hats.Gray_Hat)
|
||||
utils.mv(0, 0)
|
||||
change_hat(Hats.Dinosaur_Hat)
|
||||
|
||||
def run():
|
||||
for i in range(N):
|
||||
if i%2 == 0:
|
||||
if not utils.mv(i, 1):
|
||||
init()
|
||||
return
|
||||
if not utils.mv(i, N-1):
|
||||
init()
|
||||
return
|
||||
else:
|
||||
if not utils.mv(i, N-1):
|
||||
init()
|
||||
return
|
||||
if not utils.mv(i, 1):
|
||||
init()
|
||||
return
|
||||
if not utils.mv(N-1, 0):
|
||||
init()
|
||||
return
|
||||
if not utils.mv(0, 0):
|
||||
init()
|
||||
return
|
||||
|
||||
return (init, run)
|
||||
|
||||
def check():
|
||||
def init():
|
||||
pass
|
||||
|
||||
def run():
|
||||
if num_items(Items.Bone) >= 33488928:
|
||||
return True
|
||||
|
||||
return (init, run)
|
||||
|
||||
if __name__ == '__main__':
|
||||
set_world_size(2)
|
||||
solve.run([
|
||||
growlong(),
|
||||
# check(),
|
||||
])()
|
||||
Reference in New Issue
Block a user