diff --git a/day20/part2.txt b/day20/part2.txt new file mode 100644 index 0000000..6c8c4f0 --- /dev/null +++ b/day20/part2.txt @@ -0,0 +1,6 @@ +Part 2 wasn't really code-based. I made a graphviz visualization of my input (script to do so is in this repo) +and then I stared at it with intent, until I realized there were four "sections" that needed to line up at the +same time. I realized this seemed like an LCM problem, so I found all the times that the "sections" emitted +to the central conjunctions, and took the LCM of that, since this is cyclical. + +I left my print statements in the solution so you can try it :) diff --git a/day20/todot.py b/day20/todot.py new file mode 100644 index 0000000..58a3949 --- /dev/null +++ b/day20/todot.py @@ -0,0 +1,45 @@ +import sys +import typing +import string + +def parseModules(f: typing.TextIO) -> dict[str, list[str]]: + modules = {} + for line in f: + (key, lineModules) = line.strip().split(' -> ') + modules[key] = lineModules.split(', ') + + for fullName in modules: + for children in modules.values(): + for (i, module) in enumerate(children): + if fullName == f"&{module}" or fullName == f"%{module}": + children[i] = fullName + + return modules + +def alpha_only(s: str) -> str: + return ''.join(char for char in s if char in string.ascii_letters) + + +def main(): + if len(sys.argv) != 2: + print(f"Usage: {sys.argv[0]} input.txt") + + with open(sys.argv[1]) as f: + modules = parseModules(f) + + import pprint + print("graph {") + for node in modules.keys(): + print(f' {alpha_only(node)}[label="{node}"]') + + for parent, children in modules.items(): + print(f" {alpha_only(parent)} -> {{{' '.join(alpha_only(child) for child in children)}}}") + + print("}") + + + + + +if __name__ == "__main__": + main()