67 lines
1.9 KiB
Python
67 lines
1.9 KiB
Python
from utils.load import get_input
|
|
from utils.day import DayBase
|
|
from dataclasses import dataclass
|
|
import logging
|
|
|
|
@dataclass
|
|
class Day01(DayBase):
|
|
def __init__(self, *args, **kargs):
|
|
super().__init__(*args, **kargs)
|
|
|
|
def parse_data(self, input: str):
|
|
return input.splitlines()
|
|
|
|
def solve_part1(self, data: list[str])-> int:
|
|
count = 0
|
|
current = 50
|
|
|
|
for value in data:
|
|
if value[0] == 'L':
|
|
current = (current - int(value[1:])) % 100
|
|
else:
|
|
current = (current + int(value[1:])) % 100
|
|
|
|
if current == 0:
|
|
count += 1
|
|
logging.debug("%s -> %s (count: %s)", value, current, count)
|
|
return count
|
|
|
|
def solve_part2(self, data: list[str]) -> int:
|
|
count = 0
|
|
current = 50
|
|
for value in data:
|
|
if value[0] == 'L':
|
|
value = - int(value[1:])
|
|
else:
|
|
value = int(value[1:])
|
|
|
|
new_current = current + value
|
|
div = abs(new_current) // 100
|
|
nb_rotation = div
|
|
if new_current < 0:
|
|
if current != 0:
|
|
nb_rotation += 1
|
|
new_current += 100 * nb_rotation
|
|
elif new_current > 0:
|
|
new_current -= 100 * nb_rotation
|
|
else:
|
|
nb_rotation = 1
|
|
new_current %= 100
|
|
|
|
print(f"{current} + {value} = {new_current} (+{nb_rotation})")
|
|
current = new_current
|
|
count += nb_rotation
|
|
|
|
return count
|
|
|
|
def test_custom_2_1(self):
|
|
return self.solve_part2(["R48", "L498"]) == 5
|
|
|
|
def test_custom_2_2(self):
|
|
assert self.solve_part2(["R1000"]) == 10
|
|
|
|
if __name__ == "__main__":
|
|
day = Day01(1, "L68\nL30\nR48\nL5\nR60\nL55\nL1\nL99\nR14\nL82", excepted_part1 = 3, excepted_part2=6)
|
|
day.run_tests()
|
|
day.run()
|