Day 02 - Python - Working
This commit is contained in:
parent
c84f7b9fa0
commit
c42db9e12e
|
|
@ -0,0 +1 @@
|
||||||
|
194-253,81430782-81451118,7709443-7841298,28377-38007,6841236050-6841305978,2222204551-2222236166,2623-4197,318169-385942,9827-16119,580816-616131,646982-683917,147-181,90-120,3545483464-3545590623,4304-5747,246071-314284,8484833630-8484865127,743942-795868,42-53,1435-2086,50480-60875,16232012-16441905,94275676-94433683,61509567-61686956,3872051-4002614,6918792899-6918944930,77312-106847,282-387,829099-1016957,288251787-288311732,6271381-6313272,9877430-10095488,59-87,161112-224439,851833788-851871307,6638265-6688423,434-624,1-20,26-40,6700-9791,990-1307,73673424-73819233
|
||||||
|
|
@ -0,0 +1,69 @@
|
||||||
|
from utils.load import get_input
|
||||||
|
import unittest
|
||||||
|
from unittest import TestCase
|
||||||
|
import logging
|
||||||
|
|
||||||
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
|
|
||||||
|
|
||||||
|
def solve_part1(pairs: list[list[str]]) -> int:
|
||||||
|
sum = 0
|
||||||
|
for lower, upper in pairs:
|
||||||
|
for n in range(int(lower), int(upper) + 1):
|
||||||
|
n_str = str(n)
|
||||||
|
n_len = len(n_str)
|
||||||
|
if n_len % 2 != 0:
|
||||||
|
continue
|
||||||
|
|
||||||
|
n_half = n_len // 2
|
||||||
|
|
||||||
|
if n_str[:n_half] == n_str[n_half:]:
|
||||||
|
logging.info("Found %s", n)
|
||||||
|
sum += n
|
||||||
|
|
||||||
|
return sum
|
||||||
|
|
||||||
|
|
||||||
|
def solve_part2(pairs: list[list[str]]) -> int:
|
||||||
|
sum = 0
|
||||||
|
for lower, upper in pairs:
|
||||||
|
for n in range(int(lower), int(upper) + 1):
|
||||||
|
n_str = str(n)
|
||||||
|
n_len = len(n_str)
|
||||||
|
n_half = n_len // 2
|
||||||
|
|
||||||
|
found = False
|
||||||
|
for offset in range(n_half):
|
||||||
|
if found:
|
||||||
|
break
|
||||||
|
for pattern_length in range(1, n_half + 1):
|
||||||
|
if n_len % pattern_length != 0:
|
||||||
|
continue
|
||||||
|
pattern = n_str[offset : offset + pattern_length]
|
||||||
|
if f"{pattern}" * (n_len // pattern_length) == n_str:
|
||||||
|
logging.info("Found %s", n_str)
|
||||||
|
sum += n
|
||||||
|
found = True
|
||||||
|
break
|
||||||
|
|
||||||
|
return sum
|
||||||
|
|
||||||
|
|
||||||
|
class TestDay01(TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
input = "11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124"
|
||||||
|
pairs = input.split(",")
|
||||||
|
self.data = [pair.split("-", 2) for pair in pairs]
|
||||||
|
|
||||||
|
def __test_part_1(self):
|
||||||
|
self.assertEqual(solve_part1(self.data), 1227775554)
|
||||||
|
|
||||||
|
def test_part_2(self):
|
||||||
|
self.assertEqual(solve_part2(self.data), 4174379265)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
data = [pair.split("-", 2) for pair in get_input(2).splitlines()[0].split(",")]
|
||||||
|
#logging.info("Part1 %s", solve_part1(data))
|
||||||
|
#logging.info("Part2 %s", solve_part2(data))
|
||||||
|
unittest.main()
|
||||||
Loading…
Reference in New Issue