Day 12: Hot Springs
Megathread guidelines
- Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
- Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ , pastebin, or github (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)
FAQ
- What is this?: Here is a post with a large amount of details: https://programming.dev/post/6637268
- Where do I participate?: https://adventofcode.com/
- Is there a leaderboard for the community?: We have a programming.dev leaderboard with the info on how to join in this post: https://programming.dev/post/6631465
🔒 Thread is locked until there’s at least 100 2 star entries on the global leaderboard
🔓 Unlocked after 25 mins
Python
Also on Github.
Let me know if you have any questions or feedback!
import dataclasses import functools from .solver import Solver class MatchState: pass @dataclasses.dataclass class NotMatching(MatchState): pass @dataclasses.dataclass class Matching(MatchState): current_length: int desired_length: int @functools.cache def _match_one_template(template: str, groups: tuple[int, ...]) -> int: if not groups: if '#' in template: return 0 else: return 1 state: MatchState = NotMatching() remaining_groups: list[int] = list(groups) options_in_other_branches: int = 0 for i in range(len(template)): match (state, template[i]): case (NotMatching(), '.'): pass case (NotMatching(), '?'): options_in_other_branches += _match_one_template(template[i+1:], tuple(remaining_groups)) if not remaining_groups: return options_in_other_branches group, *remaining_groups = remaining_groups state = Matching(1, group) case (NotMatching(), '#'): if not remaining_groups: return options_in_other_branches group, *remaining_groups = remaining_groups state = Matching(1, group) case (Matching(current_length, desired_length), '.') if current_length == desired_length: state = NotMatching() case (Matching(current_length, desired_length), '.') if current_length < desired_length: return options_in_other_branches case (Matching(current_length, desired_length), '?') if current_length == desired_length: state = NotMatching() case (Matching(current_length, desired_length), '?') if current_length < desired_length: state = Matching(current_length + 1, desired_length) case (Matching(current_length, desired_length), '#') if current_length < desired_length: state = Matching(current_length + 1, desired_length) case (Matching(current_length, desired_length), '#') if current_length == desired_length: return options_in_other_branches case _: raise RuntimeError(f'unexpected {state=} with {template=} position {i} and {remaining_groups=}') match state, remaining_groups: case NotMatching(), []: return options_in_other_branches + 1 case Matching(current, desired), [] if current == desired: return options_in_other_branches + 1 case (NotMatching(), _) | (Matching(_, _), _): return options_in_other_branches raise RuntimeError(f'unexpected {state=} with {template=} at end of template and {remaining_groups=}') def _unfold(template: str, groups: tuple[int, ...]) -> tuple[str, tuple[int, ...]]: return '?'.join([template] * 5), groups * 5 class Day12(Solver): def __init__(self): super().__init__(12) self.input: list[tuple[str, tuple[int]]] = [] def presolve(self, input: str): lines = input.rstrip().split('\n') for line in lines: template, groups = line.split(' ') self.input.append((template, tuple(int(group) for group in groups.split(',')))) def solve_first_star(self) -> int: return sum(_match_one_template(template, groups) for template, groups in self.input) def solve_second_star(self) -> int: return sum(_match_one_template(*_unfold(template, groups)) for template, groups in self.input)