aoc-2015/day3/so2.py

52 lines
1 KiB
Python
Raw Permalink Normal View History

2020-11-30 16:05:48 +00:00
NORTH = "^"
SOUTH = "v"
WEST = "<"
EAST = ">"
class Santa:
def __init__(self, visits): # visits is a ref
self.pos = [0,0]
self.visits = visits
self.visits.add((self.pos[0], self.pos[1]))
def move(self, d):
if d == NORTH:
self.pos[1] += 1
if d == SOUTH:
self.pos[1] -= 1
if d == WEST:
self.pos[0] -= 1
if d == EAST:
self.pos[0] += 1
def visit(self, d):
self.move(d)
self.visits.add((self.pos[0], self.pos[1]))
def visit(l):
visits = set()
robo = Santa(visits)
santa = Santa(visits)
for i,c in enumerate(l):
if i%2 == 0:
santa.visit(c)
else:
robo.visit(c)
return len(visits)
def test():
assert visit("^v") == 3
assert visit("^>v<") == 3
assert visit("^v^v^v^v^v") == 11
def solution():
with open("in2") as puzzle:
l = puzzle.read()
return visit(l)
if __name__ == "__main__":
print(f"Houses: {solution()}")