advent_of_code_2025/python/day1.py

73 lines
1.9 KiB
Python

from utils.load import get_input
import unittest
from unittest import TestCase
import logging
def solve_part1(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(data: list[str]) -> int:
print("---------------")
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
class TestDay01(TestCase):
def test_part_1(self):
data = [ "L68", "L30", "R48", "L5", "R60", "L55", "L1", "L99", "R14", "L82" ]
self.assertEqual(solve_part1(data), 3)
def test_part_2(self):
data = [ "L68", "L30", "R48", "L5", "R60", "L55", "L1", "L99", "R14", "L82" ]
self.assertEqual(solve_part2(data), 6)
def test_custom_2(self):
data = ["R48", "L498"]
self.assertEqual(solve_part2(data), 5)
data = ["R1000"]
self.assertEqual(solve_part2(data), 10)
if __name__ == "__main__":
#unittest.main()
data = get_input(1).splitlines()
print("Part 1:", solve_part1(data))
print("Part 2:", solve_part2(data))