mirror of
https://github.com/thib8956/advent-of-code.git
synced 2025-08-24 08:21:57 +00:00
2024 day 5 part 1
This commit is contained in:
31
2024/day5/day5.py
Normal file
31
2024/day5/day5.py
Normal 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())
|
||||
|
Reference in New Issue
Block a user