Skip to content

Instantly share code, notes, and snippets.

@epitron
Created January 16, 2016 09:08
Show Gist options
  • Save epitron/76ba176f9e91d4726925 to your computer and use it in GitHub Desktop.
Save epitron/76ba176f9e91d4726925 to your computer and use it in GitHub Desktop.
lookup = {'N' : (1,0), 'S' : (-1,0), 'W' : (0,-1), 'E' : (0,1)}
def tuple_add(a, b):
return (a[0] + b[0], a[1] + b[1])
def solve(data):
pos = (0,0)
solution = 0
vertices = set([pos])
fences = set()
on_line = False
for instruct in data.upper():
new_pos = tuple_add(pos, lookup[instruct])
edge = frozenset([pos, new_pos])
if on_line is False and edge in fences:
solution += 1
on_line = True
elif new_pos in vertices and edge not in fences:
solution += 1
on_line = True
elif edge not in fences:
on_line = False
fences.add(edge)
vertices.add(new_pos)
pos = new_pos
return solution
print(solve("wwweessnnesensensenweswensewnenwenewwwwwwwennnnsssssswe"))
# with open("gates.in", "r") as infile:
# N = int(infile.readline())
# data = infile.readline()
# #assert len(data) == N
# with open("gates.out", "w") as outfile:
# outfile.write(str(solution))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment