mirror of
https://github.com/thib8956/advent-of-code.git
synced 2024-12-25 21:36:29 +00:00
2024 day 5 part 1
This commit is contained in:
parent
ba22d6c596
commit
3c211a9949
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())
|
||||||
|
|
28
2024/day5/example.txt
Normal file
28
2024/day5/example.txt
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
47|53
|
||||||
|
97|13
|
||||||
|
97|61
|
||||||
|
97|47
|
||||||
|
75|29
|
||||||
|
61|13
|
||||||
|
75|53
|
||||||
|
29|13
|
||||||
|
97|29
|
||||||
|
53|29
|
||||||
|
61|53
|
||||||
|
97|53
|
||||||
|
61|29
|
||||||
|
47|13
|
||||||
|
75|47
|
||||||
|
97|75
|
||||||
|
47|61
|
||||||
|
75|61
|
||||||
|
47|29
|
||||||
|
75|13
|
||||||
|
53|13
|
||||||
|
|
||||||
|
75,47,61,53,29
|
||||||
|
97,61,53,29,13
|
||||||
|
75,29,13
|
||||||
|
75,97,47,61,53
|
||||||
|
61,13,29
|
||||||
|
97,13,75,29,47
|
Loading…
Reference in New Issue
Block a user