2025(03): make code a bit better

And clean up part 2 of day 2, add testinput for day 1.
This commit is contained in:
2025-12-03 09:03:02 +01:00
parent 5f102fc380
commit 06d839fe7e
3 changed files with 22 additions and 50 deletions

10
2025/01/testinput Normal file
View File

@ -0,0 +1,10 @@
L68
L30
R48
L5
R60
L55
L1
L99
R14
L82

View File

@ -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)

View File

@ -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