| @@ -0,0 +1,39 @@ | |||
| # encoding: utf-8 | |||
| # Given a list of comma-separated pairs of numbers separated by a dash | |||
| # e.g. 111-999,22-33 | |||
| # find all the numbers that are some sequence of digits repeated twice | |||
| import logging | |||
| logger = logging.Logger(__name__) | |||
| logger.addHandler(logging.StreamHandler()) | |||
| logger.setLevel(logging.DEBUG) | |||
| def main(): | |||
| with open("day2_input.txt", "r") as f: | |||
| input_line = f.readline().strip() | |||
| inranges = [x.strip() for x in input_line.split(",")] | |||
| num_ranges = len(inranges) | |||
| tenths = num_ranges//10 | |||
| logger.debug(f"Received {num_ranges} ranges.") | |||
| all_invalids = [] | |||
| for i, r in enumerate(inranges): | |||
| if tenths and i % tenths == 0: | |||
| logger.debug(f"Processing range {i}.") | |||
| x, y = [int(q) for q in r.strip().split("-")] | |||
| invalids = [] | |||
| for k in range(x,y+1): | |||
| s_k = str(k) | |||
| l = len(s_k) | |||
| m = l//2 | |||
| if l % 2 == 0: | |||
| if s_k[:m] == s_k[m:]: | |||
| logger.debug(f"Found a double: {k}") | |||
| invalids.append(k) | |||
| all_invalids.extend(invalids) | |||
| logger.info(f"Found {len(all_invalids)} invalid IDs.") | |||
| logger.info(f"Sum of invalid IDs is {sum(all_invalids)}.") | |||
| if __name__ == "__main__": | |||
| main() | |||
| @@ -0,0 +1,57 @@ | |||
| # encoding: utf-8 | |||
| # Given a list of comma-separated pairs of numbers separated by a dash | |||
| # e.g. 111-999,22-33 | |||
| # find all the numbers that are some sequence of digits repeated | |||
| # any number of times | |||
| import logging | |||
| import random | |||
| import re | |||
| logger = logging.Logger(__name__) | |||
| logger.addHandler(logging.StreamHandler()) | |||
| logger.setLevel(logging.INFO) | |||
| TESTING = False | |||
| INPUT_FILE = "day2_test_input.txt" if TESTING else "day2_input.txt" | |||
| def main(): | |||
| with open(INPUT_FILE, "r") as f: | |||
| input_line = f.readline().strip() | |||
| inranges = [x.strip() for x in input_line.split(",")] | |||
| num_ranges = len(inranges) | |||
| tenths = num_ranges//10 | |||
| logger.debug(f"Received {num_ranges} ranges.") | |||
| all_invalids = [] | |||
| for i, r in enumerate(inranges): | |||
| if tenths and i % tenths == 0: | |||
| logger.debug(f"Processing range {i}.") | |||
| x, y = [int(q) for q in r.strip().split("-")] | |||
| logger.debug(f"x: {x}, y: {y}") | |||
| invalids = [] | |||
| k = x | |||
| while k <= y: | |||
| found = False | |||
| s_k = str(k) | |||
| l = len(s_k) | |||
| m = l//2 | |||
| t = 1 | |||
| while not found and t <= m: | |||
| pattern = fr'^({s_k[:t]}){{2,}}$' | |||
| found = re.match(pattern, s_k) is not None | |||
| # if random.randint(1,10) == 1: | |||
| logger.debug(f"Pattern: {pattern}, value: {s_k}, found: {found}") | |||
| if found: | |||
| logger.debug(f"Found a multiple: {k}") | |||
| invalids.append(k) | |||
| t += 1 | |||
| k += 1 | |||
| all_invalids.extend(invalids) | |||
| logger.info(f"Found {len(all_invalids)} invalid IDs.") | |||
| logger.info(f"Sum of invalid IDs is {sum(all_invalids)}.") | |||
| if __name__ == "__main__": | |||
| main() | |||
| @@ -0,0 +1 @@ | |||
| 2157315-2351307,9277418835-9277548385,4316210399-4316270469,5108-10166,872858020-872881548,537939-575851,712-1001,326613-416466,53866-90153,907856-1011878,145-267,806649-874324,6161532344-6161720341,1-19,543444404-543597493,35316486-35418695,20-38,84775309-84908167,197736-309460,112892-187377,336-552,4789179-4964962,726183-793532,595834-656619,1838-3473,3529-5102,48-84,92914229-92940627,65847714-65945664,64090783-64286175,419838-474093,85-113,34939-52753,14849-30381 | |||
| @@ -0,0 +1 @@ | |||
| 11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124 | |||