diff --git a/2024/day5/day5.py b/2024/day5/day5.py new file mode 100644 index 0000000..284f27c --- /dev/null +++ b/2024/day5/day5.py @@ -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()) + diff --git a/2024/day5/example.txt b/2024/day5/example.txt new file mode 100644 index 0000000..9d146d6 --- /dev/null +++ b/2024/day5/example.txt @@ -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