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

56 lines
1.3 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
# @shared.profile
def part1(rows):
rules, updates = read(rows)
bad = []
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:
bad.append(idx)
break
total = 0
for idx, update in enumerate(updates):
if idx in bad:
continue
total += update[len(update) // 2]
print(total)
# @shared.profile
def part2(rows):
pass
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()