You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
850 B
Python

file = open("in.txt", "r")
lns = file.readlines()
def p1():
depth = 0
horizontal = 0
for i in range(len(lns)):
d = lns[i].split()[0]
v = int(lns[i].split()[1])
if d == 'down':
depth += v
elif d == 'up':
depth -= v
elif d == 'forward':
horizontal += v
return {"depth": depth, "horizontal": horizontal}
def p2():
depth = 0
horizontal = 0
aim = 0
for ln in lns:
d = ln.split()[0]
v = int(ln.split()[1])
if d == 'down':
aim += v
elif d == 'up':
aim -= v
elif d == 'forward':
horizontal += v
depth += aim * v
return {"depth": depth, "horizontal": horizontal}
# output lines
# one = p1()
# two = p2()
# d1 = one['depth']
# h1 = one['horizontal']
# d2 = two['depth']
# h2 = two['horizontal']
# print(f"part 1: {d1 * h1}")
# print(f"part 2: {d2 * h2}")