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:
10
2025/01/testinput
Normal file
10
2025/01/testinput
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
L68
|
||||||
|
L30
|
||||||
|
R48
|
||||||
|
L5
|
||||||
|
R60
|
||||||
|
L55
|
||||||
|
L1
|
||||||
|
L99
|
||||||
|
R14
|
||||||
|
L82
|
||||||
@ -1,24 +1,16 @@
|
|||||||
def is_invalid_id(num):
|
def is_invalid_id(num):
|
||||||
# print("Testing", num)
|
|
||||||
num_str = str(num)
|
num_str = str(num)
|
||||||
num_len = len(num_str)
|
num_len = len(num_str)
|
||||||
for period in range(1, num_len):
|
for period in range(1, num_len):
|
||||||
# print("period =", period)
|
|
||||||
# Period should be a factor of num_len
|
|
||||||
if num_len % period != 0:
|
if num_len % period != 0:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Start with True, make False when it fails
|
# Start with True, make False when it fails
|
||||||
palin = True
|
palin = True
|
||||||
for idx in range(period, num_len, period):
|
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]:
|
if num_str[:period] != num_str[idx:idx + period]:
|
||||||
# print(False)
|
|
||||||
palin = False
|
palin = False
|
||||||
# print(False)
|
|
||||||
if palin:
|
if palin:
|
||||||
# print("Returning palin")
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
# No period has yielded repetition:
|
# No period has yielded repetition:
|
||||||
@ -43,22 +35,5 @@ if __name__ == "__main__":
|
|||||||
print(num, "is a palindrome thing")
|
print(num, "is a palindrome thing")
|
||||||
illegal_counter += 1
|
illegal_counter += 1
|
||||||
illegal_sum += num
|
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)
|
print(illegal_counter, illegal_sum)
|
||||||
|
|||||||
@ -1,39 +1,26 @@
|
|||||||
# I did not do anything to prevent the main code from running in part1.
|
# I did not do anything to prevent the main code from running in part1.
|
||||||
#from part1 import bruteforce_joltage
|
#from part1 import bruteforce_joltage
|
||||||
|
|
||||||
debug=False
|
|
||||||
|
|
||||||
def joltage_limit_safety_override(battery, jol_len=12):
|
def joltage_limit_safety_override(battery, jol_len=12):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
jol_indices = []
|
jol_indices = []
|
||||||
idx_seek_start = 0
|
idx_seek_start = 0
|
||||||
for i in range(jol_len):
|
for i in range(jol_len):
|
||||||
if debug:
|
# Define where to search:
|
||||||
print(f"Look for number {i}")
|
# * 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.
|
# Find, in order, the highest possible number allowed.
|
||||||
for num in range(10):
|
needle = max(subbattery)
|
||||||
needle = str(9 - num)
|
idx_needle = subbattery.index(needle)
|
||||||
if debug:
|
|
||||||
print(f"Try to find number {needle} in:")
|
# Add needle index to list and update starting point
|
||||||
subbattery = battery[idx_seek_start:-(jol_len - i + 0)]
|
jol_indices.append(idx_needle + idx_seek_start)
|
||||||
if debug:
|
idx_seek_start = idx_needle + idx_seek_start + 1
|
||||||
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])))
|
|
||||||
return int("".join([battery[i] for i in jol_indices]))
|
return int("".join([battery[i] for i in jol_indices]))
|
||||||
|
|
||||||
# Test testinput
|
# Test testinput
|
||||||
|
|||||||
Reference in New Issue
Block a user