From 06d839fe7ee1f205bf8436e18b513e480dabd170 Mon Sep 17 00:00:00 2001 From: Kees van Kempen Date: Wed, 3 Dec 2025 09:03:02 +0100 Subject: [PATCH] 2025(03): make code a bit better And clean up part 2 of day 2, add testinput for day 1. --- 2025/01/testinput | 10 ++++++++++ 2025/02/part2.py | 25 ------------------------- 2025/03/part2.py | 37 ++++++++++++------------------------- 3 files changed, 22 insertions(+), 50 deletions(-) create mode 100644 2025/01/testinput diff --git a/2025/01/testinput b/2025/01/testinput new file mode 100644 index 0000000..53287c7 --- /dev/null +++ b/2025/01/testinput @@ -0,0 +1,10 @@ +L68 +L30 +R48 +L5 +R60 +L55 +L1 +L99 +R14 +L82 diff --git a/2025/02/part2.py b/2025/02/part2.py index 1ac3fc6..86e78fe 100644 --- a/2025/02/part2.py +++ b/2025/02/part2.py @@ -1,24 +1,16 @@ def is_invalid_id(num): - # print("Testing", num) num_str = str(num) num_len = len(num_str) for period in range(1, num_len): - # print("period =", period) - # Period should be a factor of num_len if num_len % period != 0: continue # Start with True, make False when it fails palin = True for idx in range(period, num_len, period): - # print("idx =", idx) - # print(num_str[:period], "=?=", num_str[idx:idx + period]) if num_str[:period] != num_str[idx:idx + period]: - # print(False) palin = False - # print(False) if palin: - # print("Returning palin") return True # No period has yielded repetition: @@ -43,22 +35,5 @@ if __name__ == "__main__": print(num, "is a palindrome thing") illegal_counter += 1 illegal_sum += num - # num_str = str(num) - # num_len = len(num_str) - # palin = False - # # Look at the different options for lengths - # for partlen in range(2, num_len + 1): - # # Only continue if it's a factor - # if num_len % partlen != 0: - # continue - # for idx in range(partlen, num_len, partlen): - # if num_str[:partlen] == num_str[idx:idx + partlen]: - # continue - # palin = True - # break - # if palin: - # print(num_str, "is a palindrome thing") - # illegal_counter += 1 - # illegal_sum += num print(illegal_counter, illegal_sum) diff --git a/2025/03/part2.py b/2025/03/part2.py index cbdab46..317f285 100644 --- a/2025/03/part2.py +++ b/2025/03/part2.py @@ -1,39 +1,26 @@ # I did not do anything to prevent the main code from running in part1. #from part1 import bruteforce_joltage -debug=False - def joltage_limit_safety_override(battery, jol_len=12): """ - + """ jol_indices = [] idx_seek_start = 0 for i in range(jol_len): - if debug: - print(f"Look for number {i}") + # Define where to search: + # * after the found numbers + # * but leaving enough numbers for the other seeks + subbattery = battery[idx_seek_start:-(jol_len - i + 0)] + # Find, in order, the highest possible number allowed. - for num in range(10): - needle = str(9 - num) - if debug: - print(f"Try to find number {needle} in:") - subbattery = battery[idx_seek_start:-(jol_len - i + 0)] - if debug: - print(subbattery) - if not needle in subbattery: - if debug: - print("not found") - continue - idx_needle = subbattery.index(needle) - jol_indices.append(idx_needle + idx_seek_start) - idx_seek_start = idx_needle + idx_seek_start + 1 - if debug: - print("found") - break - if debug: - print(jol_indices) - print(int("".join([battery[i] for i in jol_indices]))) + needle = max(subbattery) + idx_needle = subbattery.index(needle) + + # Add needle index to list and update starting point + jol_indices.append(idx_needle + idx_seek_start) + idx_seek_start = idx_needle + idx_seek_start + 1 return int("".join([battery[i] for i in jol_indices])) # Test testinput