Skip to content

Commit

Permalink
[2024/5] p2 solved
Browse files Browse the repository at this point in the history
  • Loading branch information
StarlitGhost committed Dec 5, 2024
1 parent df0981c commit e6809db
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
Binary file modified .aoc_tiles/tiles/2024/05.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 14 additions & 2 deletions 2024/5/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,30 @@ def validate(page_order, rule_map) -> bool:
return True


def find_middle(page_order, rule_map) -> int:
for page in page_order:
pages = rule_map[page].intersection(set(page_order))
if len(pages) == len(page_order)//2:
return page
# print(page_order, page, rule_map[page], pages)


def main():
rules, page_orders = aoc.read_sections()
rules = [list(map(int, rule.split('|'))) for rule in rules.split('\n')]
page_orders = [list(map(int, pages.split(','))) for pages in page_orders.split('\n')]
rule_map = defaultdict(list)
rule_map = defaultdict(set)
for rule in rules:
rule_map[rule[0]].append(rule[1])
rule_map[rule[0]].add(rule[1])

print("p1:", sum(page_order[len(page_order)//2]
for page_order in page_orders
if validate(page_order, rule_map)))

print("p2:", sum(find_middle(page_order, rule_map)
for page_order in page_orders
if not validate(page_order, rule_map)))


if __name__ == "__main__":
main()
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ My solutions to the yearly Advents of Code

<!-- AOC TILES BEGIN -->
<h1 align="center">
Advent of Code - 170/460 ⭐
Advent of Code - 171/460 ⭐
</h1>
<h1 align="center">
2024 - 9 ⭐ - Python
2024 - 10 ⭐ - Python
</h1>
<a href="2024/1/script.py">
<img src=".aoc_tiles/tiles/2024/01.png" width="161px">
Expand Down

0 comments on commit e6809db

Please sign in to comment.