Skip to content

Instantly share code, notes, and snippets.

@chkk525
Created June 19, 2022 02:45
Show Gist options
  • Save chkk525/04dc2f7ff0e1010b1971ab4659d263a5 to your computer and use it in GitHub Desktop.
Save chkk525/04dc2f7ff0e1010b1971ab4659d263a5 to your computer and use it in GitHub Desktop.
# https://reeborg.ca/reeborg.html
DIR_PATTERNS = [
(0, 1), # ↑
(-1, 0), # ←
(0, -1), # ↓
(1, 0) # →
]
dir_idx = None
def face_north():
"""
Turn left until the roeborg faces north,
and initialize the direction index.
"""
while not is_facing_north():
turn_left()
global dir_idx
dir_idx = 0
def turn_left_dir():
"""
Turn left and update the direction
"""
global dir_idx
dir_idx = (dir_idx + 1) % len(DIR_PATTERNS)
turn_left()
def turn_right_dir():
for _ in range(3):
turn_left_dir()
# Set initial loc
loc_h, loc_v = 0,0
# List of direction&location dictionary.
history = []
# history = [(0, -2, 3), (0, -2, 4) ...]
def move_with_record():
"""
Move while recording pos and direction.
"""
global loc_h, loc_v, history
dir_v, dir_h = DIR_PATTERNS[dir_idx]
loc_h += dir_h
loc_v += dir_v
history.append((dir_idx, loc_h, loc_v))
print(history)
move()
# Face north and set initial direction.
face_north()
dir_h, dir_v = DIR_PATTERNS[dir_idx]
while not at_goal():
if right_is_clear():
turn_right_dir()
move_with_record()
elif front_is_clear():
move_with_record()
else:
turn_left_dir()
# If noticed repeating,
if history and history[-1] in history[:-1]:
if front_is_clear():
move_with_record()
elif not right_is_clear():
turn_left_dir()
move_with_record()
else:
turn_right_dir()
history = []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment