advent-of-code/2024/python/day05.py

79 lines
2.0 KiB
Python
Raw Normal View History

2024-12-05 06:15:48 +00:00
import matrix
import shared
def read(rows):
second = False
rules = []
updates = []
for row in rows:
if second:
updates.append(list(map(int, row.split(","))))
elif row == "":
second = True
else:
rules.append(list(map(int, row.split("|"))))
return rules, updates
2024-12-05 07:11:14 +00:00
def get_bads(rules, updates):
bads = []
2024-12-05 06:15:48 +00:00
for idx, update in enumerate(updates):
for first, second in rules:
if first in update and second in update:
first_idx = update.index(first)
second_idx = update.index(second)
if first_idx > second_idx:
2024-12-05 07:11:14 +00:00
bads.append(idx)
2024-12-05 06:15:48 +00:00
break
2024-12-05 07:11:14 +00:00
return bads
# @shared.profile
def part1(rows):
rules, updates = read(rows)
bads = get_bads(rules, updates)
2024-12-05 06:15:48 +00:00
total = 0
for idx, update in enumerate(updates):
2024-12-05 07:11:14 +00:00
if idx in bads:
2024-12-05 06:15:48 +00:00
continue
total += update[len(update) // 2]
print(total)
2024-12-05 07:11:14 +00:00
def fix_bad(rules, bad):
for first, second in rules:
if first in bad and second in bad:
first_idx = bad.index(first)
second_idx = bad.index(second)
if first_idx > second_idx:
bad[first_idx], bad[second_idx] = bad[second_idx], bad[first_idx]
return bad
2024-12-05 06:15:48 +00:00
# @shared.profile
def part2(rows):
2024-12-05 07:11:14 +00:00
rules, updates = read(rows)
bads = get_bads(rules, updates)
total = 0
for idx in bads:
bad = updates[idx]
while True:
after = fix_bad(rules, bad[:])
if bad == after:
break
bad = after
total += bad[len(bad) // 2]
print(total)
2024-12-05 06:15:48 +00:00
def main():
rows = [row for row in shared.load_rows(5)]
with shared.elapsed_timer() as elapsed:
part1(rows)
print("🕒", elapsed())
rows = [row for row in shared.load_rows(5, True)]
with shared.elapsed_timer() as elapsed:
part2(rows)
print("🕒", elapsed())
if __name__ == "__main__":
main()