Skip to content

Commit

Permalink
2024 day 5 part 1 and 2
Browse files Browse the repository at this point in the history
  • Loading branch information
lancelote committed Dec 5, 2024
1 parent 1ba2039 commit de04eb7
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
- 2021 - ★★★★★ ★★★★★ ★★★★★ ★★★
- 2022 - ★★★★★ ★★★★★ ★★★★★ ★☆
- 2023 - ★★★★★ ★★★★★ ★★★★★ ★★★☆
- 2024 - ★★★★
- 2024 - ★★★★

## How to use

Expand Down
36 changes: 36 additions & 0 deletions src/year2024/day05a.py
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
44 changes: 44 additions & 0 deletions src/year2024/day05b.py
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
41 changes: 41 additions & 0 deletions tests/src/year2024/test_day05a.py
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
41 changes: 41 additions & 0 deletions tests/src/year2024/test_day05b.py
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

0 comments on commit de04eb7

Please sign in to comment.