Skip to content

Commit

Permalink
Merge pull request #117 from wimglenn/2024/05
Browse files Browse the repository at this point in the history
day 05
  • Loading branch information
wimglenn authored Dec 23, 2024
2 parents ee9150c + f49bfb3 commit 41dccc4
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
37 changes: 37 additions & 0 deletions aoc_wim/aoc2024/q05.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""
--- Day 5: Print Queue ---
https://adventofcode.com/2024/day/5
"""
from aocd import data
from collections import defaultdict
from functools import cmp_to_key


rules, updates = data.split("\n\n")
rules = [x.split("|") for x in rules.splitlines()]
updates = [x.split(",") for x in updates.splitlines()]

d = defaultdict(set)
for left, right in rules:
d[left].add(right)


def cmp(p1, p2):
if p2 in d.get(p1, []):
return -1
if p1 in d.get(p2, []):
return 1
return 0


a = b = 0
for update in updates:
pages = sorted(update, key=cmp_to_key(cmp))
mid = int(pages[len(pages) // 2])
if pages == update:
a += mid
else:
b += mid

print("answer_a:", a)
print("answer_b:", b)
30 changes: 30 additions & 0 deletions tests/2024/05/0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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
143
123

0 comments on commit 41dccc4

Please sign in to comment.