2024 day 5 part 1

This commit is contained in:
2024-12-05 10:50:29 +01:00
parent ba22d6c596
commit 3c211a9949
2 changed files with 59 additions and 0 deletions

31
2024/day5/day5.py Normal file
View File

@@ -0,0 +1,31 @@
from collections import defaultdict
from itertools import pairwise
def check_update(upd, rules):
for a, b in pairwise(upd):
if [a, b] not in rules:
return False
return True
def main(content):
order, updates = content.split("\n\n")
order = [x.split("|") for x in order.split("\n")]
updates = [x.split(",") for x in updates.rstrip().split("\n")]
part1 = 0
for update in updates:
if check_update(update, order):
middle = update[len(update)//2]
part1 += int(middle)
print("Part 1: ", part1)
if __name__ == "__main__":
import sys
infile = sys.argv[1] if 1 < len(sys.argv) else "example.txt"
with open(infile) as f:
main(f.read())