Day 3
This commit is contained in:
parent
1d568ddb2a
commit
8241fde8d1
4 changed files with 91 additions and 0 deletions
1
day3/in1
Normal file
1
day3/in1
Normal file
File diff suppressed because one or more lines are too long
1
day3/in2
Normal file
1
day3/in2
Normal file
File diff suppressed because one or more lines are too long
38
day3/so1.py
Normal file
38
day3/so1.py
Normal file
|
@ -0,0 +1,38 @@
|
|||
NORTH = "^"
|
||||
SOUTH = "v"
|
||||
WEST = "<"
|
||||
EAST = ">"
|
||||
|
||||
def move(pos, d):
|
||||
if d == NORTH:
|
||||
pos[1] += 1
|
||||
if d == SOUTH:
|
||||
pos[1] -= 1
|
||||
if d == WEST:
|
||||
pos[0] -= 1
|
||||
if d == EAST:
|
||||
pos[0] += 1
|
||||
|
||||
def visit(s):
|
||||
pos = [0,0]
|
||||
visits = set()
|
||||
visits.add((pos[0], pos[1]))
|
||||
|
||||
for c in s:
|
||||
move(pos, c)
|
||||
visits.add((pos[0], pos[1]))
|
||||
|
||||
return len(visits)
|
||||
|
||||
def test():
|
||||
assert visit(">") == 2
|
||||
assert visit("^>v<") == 4
|
||||
assert visit("^v^v^v^v^v") == 2
|
||||
|
||||
def solution():
|
||||
with open("in1") as puzzle:
|
||||
l = puzzle.read()
|
||||
return visit(l)
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(f"Houses: {solution()}")
|
51
day3/so2.py
Normal file
51
day3/so2.py
Normal file
|
@ -0,0 +1,51 @@
|
|||
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()}")
|
Loading…
Reference in a new issue