Skip to content

Commit

Permalink
2024 day 5
Browse files Browse the repository at this point in the history
  • Loading branch information
Ted Cassirer committed Dec 5, 2024
1 parent 8ecf902 commit ea80f2b
Show file tree
Hide file tree
Showing 25 changed files with 157 additions and 45 deletions.
4 changes: 2 additions & 2 deletions aoc_cas/aoc2023/day1.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ def part_b(data) -> int:


if __name__ == "__main__":
from aoc_cas.util import solve_with_examples
from aoc_cas.util import solve_with_example_data

solve_with_examples(2023, 1)
solve_with_example_data(2023, 1)
4 changes: 2 additions & 2 deletions aoc_cas/aoc2023/day10.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,6 @@ def part_b(data: str) -> int:


if __name__ == "__main__":
from aoc_cas.util import solve_with_examples
from aoc_cas.util import solve_with_example_data

solve_with_examples(year=2023, day=10)
solve_with_example_data(year=2023, day=10)
4 changes: 2 additions & 2 deletions aoc_cas/aoc2023/day11.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ def part_b(data: str, expansion: int = 1000000) -> int:


if __name__ == "__main__":
from aoc_cas.util import solve_with_examples
from aoc_cas.util import solve_with_example_data

solve_with_examples(year=2023, day=11)
solve_with_example_data(year=2023, day=11)
4 changes: 2 additions & 2 deletions aoc_cas/aoc2023/day12.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ def part_b(data: str) -> int:


if __name__ == "__main__":
from aoc_cas.util import solve_with_examples
from aoc_cas.util import solve_with_example_data

solve_with_examples(year=2023, day=12)
solve_with_example_data(year=2023, day=12)
4 changes: 2 additions & 2 deletions aoc_cas/aoc2023/day13.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def part_b(data: str) -> int:


if __name__ == "__main__":
from aoc_cas.util import solve_with_examples
from aoc_cas.util import solve_with_example_data

solve_with_examples(year=2023, day=13)
solve_with_example_data(year=2023, day=13)
["#.##..##." "..#.##.#." "##......#" "##......#" "..#.##.#." "..##..##." "#.#.##.#."]
4 changes: 2 additions & 2 deletions aoc_cas/aoc2023/day14.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,6 @@ def part_b(data: str) -> int:


if __name__ == "__main__":
from aoc_cas.util import solve_with_examples
from aoc_cas.util import solve_with_example_data

solve_with_examples(year=2023, day=14)
solve_with_example_data(year=2023, day=14)
4 changes: 2 additions & 2 deletions aoc_cas/aoc2023/day15.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ def part_b(data: str) -> int:


if __name__ == "__main__":
from aoc_cas.util import solve_with_examples
from aoc_cas.util import solve_with_example_data

solve_with_examples(year=2023, day=15)
solve_with_example_data(year=2023, day=15)
4 changes: 2 additions & 2 deletions aoc_cas/aoc2023/day16.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,6 @@ def part_b(data: str) -> int:


if __name__ == "__main__":
from aoc_cas.util import solve_with_examples
from aoc_cas.util import solve_with_example_data

solve_with_examples(year=2023, day=16)
solve_with_example_data(year=2023, day=16)
4 changes: 2 additions & 2 deletions aoc_cas/aoc2023/day17.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ def part_b(data: str) -> int:


if __name__ == "__main__":
from aoc_cas.util import solve_with_examples
from aoc_cas.util import solve_with_example_data

solve_with_examples(year=2023, day=17)
solve_with_example_data(year=2023, day=17)
4 changes: 2 additions & 2 deletions aoc_cas/aoc2023/day18.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ def part_b(data: str) -> int:


if __name__ == "__main__":
from aoc_cas.util import solve_with_examples
from aoc_cas.util import solve_with_example_data

solve_with_examples(year=2023, day=18)
solve_with_example_data(year=2023, day=18)
4 changes: 2 additions & 2 deletions aoc_cas/aoc2023/day2.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ def part_b(data: str) -> int:


if __name__ == "__main__":
from aoc_cas.util import solve_with_examples
from aoc_cas.util import solve_with_example_data

solve_with_examples(year=2023, day=2)
solve_with_example_data(year=2023, day=2)
4 changes: 2 additions & 2 deletions aoc_cas/aoc2023/day3.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ def part_b(data: str) -> int:


if __name__ == "__main__":
from aoc_cas.util import solve_with_examples
from aoc_cas.util import solve_with_example_data

solve_with_examples(year=2023, day=3)
solve_with_example_data(year=2023, day=3)
4 changes: 2 additions & 2 deletions aoc_cas/aoc2023/day4.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ def part_b(data: str) -> int:


if __name__ == "__main__":
from aoc_cas.util import solve_with_examples
from aoc_cas.util import solve_with_example_data

solve_with_examples(year=2023, day=4)
solve_with_example_data(year=2023, day=4)
4 changes: 2 additions & 2 deletions aoc_cas/aoc2023/day5.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def part_b(data: str) -> int:


if __name__ == "__main__":
from aoc_cas.util import solve_with_examples
from aoc_cas.util import solve_with_example_data

solve_with_examples(year=2023, day=5)
solve_with_example_data(year=2023, day=5)
data = get_data(year=2023, day=5)
4 changes: 2 additions & 2 deletions aoc_cas/aoc2023/day6.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ def part_b(data: str) -> int:


if __name__ == "__main__":
from aoc_cas.util import solve_with_examples
from aoc_cas.util import solve_with_example_data

solve_with_examples(year=2023, day=6)
solve_with_example_data(year=2023, day=6)
4 changes: 2 additions & 2 deletions aoc_cas/aoc2023/day7.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,6 @@ def part_b(data: str) -> int:


if __name__ == "__main__":
from aoc_cas.util import solve_with_examples
from aoc_cas.util import solve_with_example_data

solve_with_examples(year=2023, day=7)
solve_with_example_data(year=2023, day=7)
4 changes: 2 additions & 2 deletions aoc_cas/aoc2023/day8.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ def part_b(data: str) -> int:


if __name__ == "__main__":
from aoc_cas.util import solve_with_examples
from aoc_cas.util import solve_with_example_data

solve_with_examples(year=2023, day=8)
solve_with_example_data(year=2023, day=8)
4 changes: 2 additions & 2 deletions aoc_cas/aoc2023/day9.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ def part_b(data: str) -> int:


if __name__ == "__main__":
from aoc_cas.util import solve_with_examples
from aoc_cas.util import solve_with_example_data

solve_with_examples(year=2023, day=9)
solve_with_example_data(year=2023, day=9)
4 changes: 2 additions & 2 deletions aoc_cas/aoc2024/day1.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ def part_b(data: str) -> int:


if __name__ == "__main__":
from aoc_cas.util import solve_with_examples
from aoc_cas.util import solve_with_example_data

solve_with_examples(year=2024, day=1)
solve_with_example_data(year=2024, day=1)
4 changes: 2 additions & 2 deletions aoc_cas/aoc2024/day2.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ def part_b(data: str) -> int:


if __name__ == "__main__":
from aoc_cas.util import solve_with_examples
from aoc_cas.util import solve_with_example_data

solve_with_examples(year=2024, day=2)
solve_with_example_data(year=2024, day=2)
4 changes: 2 additions & 2 deletions aoc_cas/aoc2024/day3.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ def part_b(data: str) -> int:


if __name__ == "__main__":
from aoc_cas.util import solve_with_examples
from aoc_cas.util import solve_with_example_data

solve_with_examples(year=2024, day=3)
solve_with_example_data(year=2024, day=3)
69 changes: 69 additions & 0 deletions aoc_cas/aoc2024/day5.py
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)
6 changes: 4 additions & 2 deletions aoc_cas/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ def solve(year: int, day: int, submit: bool) -> None:
print(f"PartA: {part_a_result}")
print(f"PartB: {part_b_result}")
if submit:
puzzle.answer_a = part_a_result
puzzle.answer_b = part_b_result
if part_a_result is not None:
puzzle.answer_a = part_a_result
if part_b_result is not None:
puzzle.answer_b = part_b_result


@main.command()
Expand Down
13 changes: 12 additions & 1 deletion aoc_cas/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,18 @@ def load_module(year: int, day: int):
return importlib.import_module(module_name)


def solve_with_examples(year: int, day: int) -> None:
def solve_with_real_input_data(year: int, day: int) -> None:
puzzle = Puzzle(year, day)
mod = load_module(year, day)
part_a_result = str(mod.part_a(puzzle.input_data))
part_b_result = str(mod.part_b(puzzle.input_data))

print(f"\nReal data input\n")
print(f"[Part A] {part_a_result}")
print(f"[Part B] {part_b_result}")


def solve_with_example_data(year: int, day: int) -> None:
puzzle = Puzzle(year, day)
print(f"Testing example data")

Expand Down
30 changes: 30 additions & 0 deletions tests/fixtures/2024/5.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 ea80f2b

Please sign in to comment.