Solve day 1, part 1
This commit is contained in:
22
day1/part1.py
Normal file
22
day1/part1.py
Normal file
@@ -0,0 +1,22 @@
|
||||
file = "input1.txt"
|
||||
|
||||
def main():
|
||||
current = 50
|
||||
count = 0
|
||||
print(f"{current} count:{count}")
|
||||
for line in open(file):
|
||||
direction, magnitude = line[:1], int(line[1:])
|
||||
scalar = 1 if direction == "R" else -1
|
||||
current = normalize(current + scalar * magnitude)
|
||||
if current == 0: count += 1
|
||||
print(f"{direction} {magnitude} => {current} count:{count}")
|
||||
|
||||
def normalize(x):
|
||||
if 0 <= x <= 99: return x
|
||||
if x <= 0:
|
||||
return normalize(100 + x)
|
||||
else:
|
||||
return normalize(x - 100)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user