-
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
Showing
5 changed files
with
163 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
"""2024 - Day 5 Part 1: ...""" | ||
|
||
from collections import defaultdict | ||
|
||
|
||
def is_valid(update: list[int], rules: dict[int, set[int]]) -> bool: | ||
for i in range(len(update) - 1): | ||
a = update[i] | ||
b = update[i + 1] | ||
|
||
if b not in rules[a]: | ||
return False | ||
|
||
return True | ||
|
||
|
||
def solve(task: str) -> int: | ||
first, second = task.split("\n\n") | ||
rules: dict[int, set[int]] = defaultdict(set) | ||
|
||
for line in first.split("\n"): | ||
a, b = line.split("|") | ||
rules[int(a)].add(int(b)) | ||
|
||
updates: list[list[int]] = [] | ||
|
||
for line in second.split("\n"): | ||
updates.append([int(x) for x in line.split(",")]) | ||
|
||
ans = 0 | ||
for update in updates: | ||
if is_valid(update, rules): | ||
middle = update[len(update) // 2] | ||
ans += middle | ||
|
||
return ans |
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,44 @@ | ||
"""2024 - Day 5 Part 2: ...""" | ||
|
||
import functools | ||
from collections import defaultdict | ||
|
||
|
||
def is_valid(update: list[int], rules: dict[int, set[int]]) -> bool: | ||
for i in range(len(update) - 1): | ||
a = update[i] | ||
b = update[i + 1] | ||
|
||
if b not in rules[a]: | ||
return False | ||
|
||
return True | ||
|
||
|
||
def solve(task: str) -> int: | ||
first, second = task.split("\n\n") | ||
rules: dict[int, set[int]] = defaultdict(set) | ||
|
||
for line in first.split("\n"): | ||
a, b = line.split("|") | ||
rules[int(a)].add(int(b)) | ||
|
||
updates: list[list[int]] = [] | ||
|
||
def compare(x: int, y: int) -> int: | ||
if y in rules[x]: | ||
return -1 | ||
else: | ||
return +1 | ||
|
||
for line in second.split("\n"): | ||
updates.append([int(x) for x in line.split(",")]) | ||
|
||
ans = 0 | ||
for update in updates: | ||
if not is_valid(update, rules): | ||
update.sort(key=functools.cmp_to_key(compare)) | ||
middle = update[len(update) // 2] | ||
ans += middle | ||
|
||
return ans |
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,41 @@ | ||
"""2024 - Day 5 Part 1: ...""" | ||
|
||
from textwrap import dedent | ||
|
||
from src.year2024.day05a import solve | ||
|
||
|
||
def test_solve(): | ||
task = dedent( | ||
""" | ||
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 | ||
""" | ||
).strip() | ||
assert solve(task) == 143 |
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,41 @@ | ||
"""2024 - Day 5 Part 2: ...""" | ||
|
||
from textwrap import dedent | ||
|
||
from src.year2024.day05b import solve | ||
|
||
|
||
def test_solve(): | ||
task = dedent( | ||
""" | ||
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 | ||
""" | ||
).strip() | ||
assert solve(task) == 123 |