46 lines
886 B
Python
46 lines
886 B
Python
from itertools import groupby
|
|
|
|
data = open("input").read().strip().split("\n")
|
|
data = [(a[0], int(a[1:])) for a in data]
|
|
|
|
fs = {"R": lambda x, y: x + y, "L": lambda x, y: x - y}
|
|
|
|
|
|
def part1(data):
|
|
s = 50
|
|
c = 0
|
|
for a, b in data:
|
|
f = fs[a]
|
|
while not 0 <= f(s, b) <= 99:
|
|
b -= 100
|
|
s = f(s, b)
|
|
if s == 0:
|
|
c += 1
|
|
return c
|
|
|
|
|
|
def part2(data):
|
|
s = 50
|
|
c = 0
|
|
for a, b in groupby(data, key=lambda x: x[0]):
|
|
x = sum(y[1] for y in b)
|
|
f = fs[a]
|
|
while not 0 <= x <= 99:
|
|
x -= 100
|
|
c += 1
|
|
while x > 0:
|
|
s = f(s, 1)
|
|
if s == 0:
|
|
c += 1
|
|
elif s == -1:
|
|
s = 99
|
|
elif s == 100:
|
|
s = 0
|
|
c += 1
|
|
x -= 1
|
|
return c
|
|
|
|
|
|
print(part1(data))
|
|
print(part2(data))
|