2024(5): I hate this less

This commit is contained in:
2024-12-19 13:56:54 +01:00
parent fee0a57fc2
commit e672c83426

View File

@ -1,7 +1,7 @@
from puzzle1 import * from puzzle1 import *
verbose = True verbose = False
def swap(lst, idx_a, idx_b): def swap(lst, idx_a, idx_b):
temp = lst[idx_a] temp = lst[idx_a]
@ -13,36 +13,21 @@ def fix_update(update, rules):
# Allow the loop to be restared after a fix. # Allow the loop to be restared after a fix.
i = 0 i = 0
update_free = True swap_free = False
while i < len(rules): while not swap_free:
lhs, rhs = rules[i] swap_free = True
# NOTE: This is too verbose. for rule in rules:
#if verbose: lhs, rhs = rule
# print(i, rules[i], end=" ")
if not lhs in update or not rhs in update: if not lhs in update or not rhs in update:
i += 1 continue
if i == len(rules) and not update_free:
i = 0
update_free = True
# NOTE: This is a copied condition from below.
continue
idx_lhs, idx_rhs = update.index(lhs), update.index(rhs) idx_lhs, idx_rhs = update.index(lhs), update.index(rhs)
if idx_lhs > idx_rhs: if idx_lhs > idx_rhs:
if verbose: if verbose:
print(f"swapping as {lhs} < {rhs}") print(f"swapping as {lhs} < {rhs}")
swap(update, idx_lhs, idx_rhs) swap(update, idx_lhs, idx_rhs)
update_free = False swap_free = False
i += 1
if i == len(rules) and not update_free:
if verbose:
print("resetting")
i = 0
update_free = True
elif i == len(rules):
print("stopping")
if __name__ == "__main__": if __name__ == "__main__":
rules, updates = load_input() rules, updates = load_input()
@ -51,7 +36,8 @@ if __name__ == "__main__":
# Let's assume numbers can only occur once (although the checker does not # Let's assume numbers can only occur once (although the checker does not
# make this assumption), and every update CAN be fixed. # make this assumption), and every update CAN be fixed.
for idx, update in enumerate(updates): for idx, update in enumerate(updates):
print(f"{idx:5d}/{len(updates)}:", update) if verbose:
print(f"{idx:5d}/{len(updates)}:", update)
if is_update_legal(update, rules): if is_update_legal(update, rules):
if verbose: if verbose:
print(update, " is legal") print(update, " is legal")