Finished day 1
This commit is contained in:
parent
ebe2c7af34
commit
c84f7b9fa0
77
src/day1.py
77
src/day1.py
|
|
@ -1,35 +1,72 @@
|
|||
from utils.load import get_input
|
||||
import unittest
|
||||
from unittest import TestCase
|
||||
import logging
|
||||
|
||||
def solve_part1():
|
||||
def solve_part1(data: list[str]) -> int:
|
||||
count = 0
|
||||
current = 50
|
||||
data = get_input(1).splitlines()
|
||||
#data = [ "L68", "L30", "R48", "L5", "R60", "L55", "L1", "L99", "R14", "L82" ]
|
||||
|
||||
for value in data:
|
||||
previous_value = current
|
||||
if value[0] == 'L':
|
||||
current = (current - int(value[1:]) )
|
||||
current = (current - int(value[1:])) % 100
|
||||
else:
|
||||
current = (current + int(value[1:]) )
|
||||
current = (current + int(value[1:])) % 100
|
||||
|
||||
nb_rotation_zero = 0
|
||||
if current > 0:
|
||||
nb_rotation_zero = current // 100
|
||||
current %= 100
|
||||
elif current < 0:
|
||||
nb_rotation_zero = 1 + abs(current) // 100
|
||||
current = current + 100 * nb_rotation_zero
|
||||
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:
|
||||
nb_rotation_zero = 1
|
||||
value = int(value[1:])
|
||||
|
||||
if previous_value == 0:
|
||||
nb_rotation_zero = max(nb_rotation_zero - 1, 0)
|
||||
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
|
||||
|
||||
count += nb_rotation_zero
|
||||
print(f"{value} -> {current} (+{nb_rotation_zero}) (count: {count})")
|
||||
print(count)
|
||||
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__":
|
||||
solve_part1()
|
||||
#unittest.main()
|
||||
|
||||
data = get_input(1).splitlines()
|
||||
print("Part 1:", solve_part1(data))
|
||||
print("Part 2:", solve_part2(data))
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue