|
| 1 | +# MIT License |
| 2 | +# |
| 3 | +# Copyright (c) 2021 Andrew Krepps |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in all |
| 13 | +# copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +# SOFTWARE. |
| 22 | + |
| 23 | + |
| 24 | +import copy |
| 25 | + |
| 26 | +from . import util |
| 27 | + |
| 28 | + |
| 29 | +def parse_input(lines): |
| 30 | + rules = {} |
| 31 | + messages = [] |
| 32 | + for line in lines: |
| 33 | + if '0' <= line[0] <= '9': |
| 34 | + colon_idx = line.index(':') |
| 35 | + rule_id = line[0:colon_idx].strip() |
| 36 | + rule_content = line[colon_idx + 1:] |
| 37 | + sub_rules = [] |
| 38 | + for sub_rule in rule_content.split('|'): |
| 39 | + sub_rules.append([rule_token.strip() for rule_token in sub_rule.split()]) |
| 40 | + rules[rule_id] = sub_rules |
| 41 | + else: |
| 42 | + messages.append(line) |
| 43 | + return rules, messages |
| 44 | + |
| 45 | + |
| 46 | +def message_matches_rule(message, rule_id, rules): |
| 47 | + final_skip_possibilities = set() |
| 48 | + if len(message) == 0: |
| 49 | + return final_skip_possibilities |
| 50 | + |
| 51 | + for sub_rule in rules[rule_id]: |
| 52 | + sub_skip_possibilities = {0} |
| 53 | + next_skips = set() |
| 54 | + for rule_token in sub_rule: |
| 55 | + for current_skip_count in sub_skip_possibilities: |
| 56 | + # assumes terminal rules are only one character |
| 57 | + if rule_token.startswith('"'): |
| 58 | + if message[current_skip_count] == rule_token[1]: |
| 59 | + next_skips.add(current_skip_count + 1) |
| 60 | + else: |
| 61 | + for new_skip in message_matches_rule(message[current_skip_count:], rule_token, rules): |
| 62 | + next_skips.add(current_skip_count + new_skip) |
| 63 | + sub_skip_possibilities, next_skips = next_skips, sub_skip_possibilities |
| 64 | + next_skips.clear() |
| 65 | + final_skip_possibilities.update(sub_skip_possibilities) |
| 66 | + |
| 67 | + return final_skip_possibilities |
| 68 | + |
| 69 | + |
| 70 | +def count_valid_messages(rules, messages): |
| 71 | + count = 0 |
| 72 | + for message in messages: |
| 73 | + skip_possibilities = message_matches_rule(message, '0', rules) |
| 74 | + if len(message) in skip_possibilities: |
| 75 | + count += 1 |
| 76 | + return count |
| 77 | + |
| 78 | + |
| 79 | +def get_part1_answer(rules, messages): |
| 80 | + return count_valid_messages(rules, messages) |
| 81 | + |
| 82 | + |
| 83 | +def get_part2_answer(rules, messages): |
| 84 | + modified_rules = copy.deepcopy(rules) |
| 85 | + modified_rules['8'] = [['42'], ['42', '8']] |
| 86 | + modified_rules['11'] = [['42', '31'], ['42', '11', '31']] |
| 87 | + return count_valid_messages(modified_rules, messages) |
| 88 | + |
| 89 | + |
| 90 | +def run(): |
| 91 | + lines = util.get_input_file_lines("day19.txt") |
| 92 | + rules, messages = parse_input(lines) |
| 93 | + print(f"The answer to part 1 is {get_part1_answer(rules, messages)}") |
| 94 | + print(f"The answer to part 2 is {get_part2_answer(rules, messages)}") |
0 commit comments