Skip to content

Commit

Permalink
add day 8
Browse files Browse the repository at this point in the history
  • Loading branch information
JasperZP committed Dec 9, 2023
1 parent 3213c22 commit 6e6650e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
*input
*input*

3 changes: 1 addition & 2 deletions 7/7.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def main(input_file: typer.FileText):
)

print(f"Part 1 total winnings: {total_winnings}")

hands = []
global card_values
global card_values_joker
Expand All @@ -170,6 +170,5 @@ def main(input_file: typer.FileText):
print(f"Part 2 total winnings: {total_winnings}")



if __name__ == "__main__":
typer.run(main)
68 changes: 68 additions & 0 deletions 8/8.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import typer
import math


def part1(nodes, instructions):
current_node = "AAA"
instructions_traversed = 0
if "AAA" not in nodes:
return
while True:
for instruction in instructions:
current_node = nodes[current_node][instruction]
instructions_traversed += 1
if current_node == "ZZZ":
break
if current_node == "ZZZ":
break

print(f"Part 1: instructions traversed: {instructions_traversed}")


def part2(nodes, instructions):
current_nodes = [node for node in nodes if node.endswith("A")]
periods = []
for current_node in current_nodes:
instructions_traversed = 0
period = None
while True:
for instruction in instructions:
current_node = nodes[current_node][instruction]
instructions_traversed += 1
if current_node.endswith("Z"):
periods.append(instructions_traversed)
break
if current_node.endswith("Z"):
break

gcd = math.gcd(*periods)
periods = [p // gcd for p in periods]
product = math.prod(periods)
instructions_traversed = product * gcd
print(f"Part2: {instructions_traversed}")


def main(input_file: typer.FileText):
input = input_file.read().split("\n")

nodes = {}
instructions = ""
for row in input:
if row == "":
continue
elif "=" in row:
values = row.split("=")
name = values[0].strip()
directions = values[1].strip("(), ").split(", ")
left = directions[0]
right = directions[1]
nodes[name] = {"L": directions[0], "R": directions[1]}
else:
instructions = row.strip()

part1(nodes, instructions)
part2(nodes, instructions)


if __name__ == "__main__":
typer.run(main)

0 comments on commit 6e6650e

Please sign in to comment.