-
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
8 changed files
with
1,476 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
Empty file.
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,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.
Empty file.
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,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 |
Oops, something went wrong.