-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ted Cassirer
committed
Dec 5, 2024
1 parent
8ecf902
commit ea80f2b
Showing
25 changed files
with
157 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import typing as t | ||
from collections import defaultdict | ||
|
||
Update = list[int] | ||
IsOrdered = t.Callable[[int, int], bool] | ||
|
||
|
||
def _build_rule(rule_data: str) -> IsOrdered: | ||
rule: dict[int, set[int]] = defaultdict(set) | ||
for line in rule_data.splitlines(): | ||
n1, n2 = map(int, line.split("|")) | ||
rule[n1].add(n2) | ||
|
||
def _is_before(n1: int, n2: int) -> bool: | ||
if n2 in rule[n1]: | ||
return True | ||
return False | ||
|
||
return _is_before | ||
|
||
|
||
def _build_page_updates(page_data: str) -> list[Update]: | ||
page_updates = [] | ||
for line in page_data.splitlines(): | ||
page_updates.append(list(map(int, line.split(",")))) | ||
return page_updates | ||
|
||
|
||
def _parse_data(data: str) -> tuple[IsOrdered, list[Update]]: | ||
rule_data, page_data = data.split("\n\n") | ||
return _build_rule(rule_data), _build_page_updates(page_data) | ||
|
||
|
||
def _update_order_is_valid(update: Update, rule: IsOrdered) -> bool: | ||
return all(rule(n1, n2) for n1, n2 in zip(update, update[1:])) | ||
|
||
|
||
def part_a(data: str) -> int: | ||
rule, updates = _parse_data(data) | ||
middle_page_sum = 0 | ||
for update in updates: | ||
if _update_order_is_valid(update, rule): | ||
middle_page_sum += update[len(update) // 2] | ||
return middle_page_sum | ||
|
||
|
||
def part_b(data: str) -> int: | ||
rule, updates = _parse_data(data) | ||
|
||
def fix_page_order(update: Update) -> Update: | ||
# Bubble sort it | ||
for k in range(1, len(update)): | ||
for i in range(len(update) - k): | ||
if not rule(update[i], update[i + 1]): | ||
update[i], update[i + 1] = update[i + 1], update[i] | ||
return update | ||
|
||
middle_page_sum = 0 | ||
for update in updates: | ||
if not _update_order_is_valid(update, rule): | ||
update = fix_page_order(update) | ||
middle_page_sum += update[len(update) // 2] | ||
return middle_page_sum | ||
|
||
|
||
if __name__ == "__main__": | ||
from aoc_cas.util import solve_with_example_data | ||
|
||
solve_with_example_data(year=2024, day=5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |