From 8a21b6b78a9f249d45d9e3bf5fc8610e51958353 Mon Sep 17 00:00:00 2001 From: Bob Date: Tue, 2 Dec 2025 13:17:40 +0100 Subject: [PATCH] day02 --- bob/day02/day02-p1.py | 41 +++++++++++++++++++++++++ bob/day02/day02-p2.py | 61 ++++++++++++++++++++++++++++++++++++++ bob/day02/example_input | 1 + bob/day02/input | 1 + bob/day02/my_example_input | 1 + 5 files changed, 105 insertions(+) create mode 100644 bob/day02/day02-p1.py create mode 100644 bob/day02/day02-p2.py create mode 100644 bob/day02/example_input create mode 100644 bob/day02/input create mode 100644 bob/day02/my_example_input diff --git a/bob/day02/day02-p1.py b/bob/day02/day02-p1.py new file mode 100644 index 0000000..2cc827f --- /dev/null +++ b/bob/day02/day02-p1.py @@ -0,0 +1,41 @@ + + + +def check_id(start, end): + false_ids = [] + for id in range(int(start), int(end)+1): + id_str = str(id) + id_str_len = len(id_str) + if id_str_len % 2 == 0: + h1 = id_str[:id_str_len//2] + h2 = id_str[id_str_len//2:] + if h1 == h2: + false_ids.append(id) + + return false_ids + + + +def read_input(filename): + id_ranges = [] + for line in open(filename).read().splitlines(): + for id_range in line.split(","): + start = id_range.split("-")[0] + end = id_range.split("-")[1] + + id_ranges.append((start,end)) + return id_ranges + + +# filename = "day02/example_input" # 1227775554 +filename = "day02/input" # 23560874270 +id_ranges = read_input(filename) + + +adding = 0 +for id_range in id_ranges: + for false_id in check_id(id_range[0], id_range[1]): + adding += false_id + + +print(f"Added total of false IDs: {adding}") diff --git a/bob/day02/day02-p2.py b/bob/day02/day02-p2.py new file mode 100644 index 0000000..ba8b327 --- /dev/null +++ b/bob/day02/day02-p2.py @@ -0,0 +1,61 @@ + + + +def check_id(start_id, end_id): + false_ids = [] + for id in range(int(start_id), int(end_id)+1): + id_str = str(id) + id_str_len = len(id_str) + + for substr_len in range(1,(id_str_len//2)+1): + if id_str_len % substr_len > 0: + # Substring being checked must fit a whole number of times + continue + + dup = False + # Substring position is determined by the amount of times the substring fits in the string + # That makes up the range to be checked (as multipliet with the substring length) + for substr_pos in range(1, id_str_len // substr_len): + h1 = id_str[:substr_len] + + # Get the substring at right position, and only with size of substring + h2 = id_str[substr_len*substr_pos:][:substr_len] + if h1 != h2: + dup = False + break + else: + dup = True + + if dup: + false_ids.append(id) + break + + + return false_ids + + + +def read_input(filename): + id_ranges = [] + for line in open(filename).read().splitlines(): + for id_range in line.split(","): + start = id_range.split("-")[0] + end = id_range.split("-")[1] + + id_ranges.append((start,end)) + return id_ranges + + +# filename = "day02/my_example_input" # +# filename = "day02/example_input" # 4174379265 +filename = "day02/input" # 44143124633 +id_ranges = read_input(filename) + + +adding = 0 +for id_range in id_ranges: + for false_id in check_id(id_range[0], id_range[1]): + adding += false_id + + +print(f"Added total of false IDs: {adding}") diff --git a/bob/day02/example_input b/bob/day02/example_input new file mode 100644 index 0000000..a3f22ef --- /dev/null +++ b/bob/day02/example_input @@ -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 diff --git a/bob/day02/input b/bob/day02/input new file mode 100644 index 0000000..5066627 --- /dev/null +++ b/bob/day02/input @@ -0,0 +1 @@ +990244-1009337,5518069-5608946,34273134-34397466,3636295061-3636388848,8613701-8663602,573252-688417,472288-533253,960590-988421,7373678538-7373794411,178-266,63577667-63679502,70-132,487-1146,666631751-666711926,5896-10827,30288-52204,21847924-21889141,69684057-69706531,97142181-97271487,538561-555085,286637-467444,93452333-93519874,69247-119122,8955190262-8955353747,883317-948391,8282803943-8282844514,214125-236989,2518-4693,586540593-586645823,137643-211684,33-47,16210-28409,748488-837584,1381-2281,1-19 diff --git a/bob/day02/my_example_input b/bob/day02/my_example_input new file mode 100644 index 0000000..a729ca2 --- /dev/null +++ b/bob/day02/my_example_input @@ -0,0 +1 @@ +12345678-12345679