Skip to content

Commit

Permalink
AoC 2024 day 5 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
loociano committed Dec 5, 2024
1 parent 6fc44a9 commit dbbd5d6
Show file tree
Hide file tree
Showing 8 changed files with 1,476 additions and 0 deletions.
Empty file added aoc2024/src/day05/__init__.py
Empty file.
Empty file.
51 changes: 51 additions & 0 deletions aoc2024/src/day05/python/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from collections import defaultdict
from typing import Sequence

# Tracks pages to downstream pages.
type RuleGraph = defaultdict[int, set]


def _generate_graph(order_rules: Sequence[str]) -> RuleGraph:
"""Generates graph from order rules. A|B means A precedes B."""
graph = defaultdict(set)
for rule in order_rules:
first, second = map(int, rule.split('|'))
graph[first].add(second)
return graph


def _is_valid(graph: RuleGraph, update: Sequence[int]) -> bool:
"""Returns true if an update (sequence of pages) complies with rules."""
if len(update) == 1:
return True
curr_page = update[0]
next_page = update[1]
if curr_page not in graph:
return False
children = graph.get(curr_page)
if next_page not in children:
return False
return _is_valid(graph, update[1:])


def sum_middle_pages(input: Sequence[str]) -> int:
"""Sums the middle page of all the valid updates."""
breakline_num = list(input).index('')
graph = _generate_graph(input[:breakline_num])
updates = (list(map(int, input[i].split(',')))
for i in range(breakline_num + 1, len(input)))
return sum(update[len(update) // 2] if _is_valid(graph, update) else 0
for update in updates)
Empty file added aoc2024/test/day05/__init__.py
Empty file.
Empty file.
28 changes: 28 additions & 0 deletions aoc2024/test/day05/python/example1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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
Loading

0 comments on commit dbbd5d6

Please sign in to comment.