Add day 20 part 2

master
Nick Krichevsky 2023-12-21 18:16:05 -05:00
parent 1957e1b39a
commit 044737353d
2 changed files with 51 additions and 0 deletions

6
day20/part2.txt Normal file
View File

@ -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 :)

45
day20/todot.py Normal file
View File

@ -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()