-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday8.py
50 lines (41 loc) · 1.37 KB
/
day8.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python3
import numpy as np
import re
from collections import defaultdict
f = open("ressources/day8.txt", "r") #Open File
lines = f.readlines() #Separate in lines
instructions = lines[0].replace("\n", "");
graph = defaultdict(list)
lengthInstruction = len(instructions)
for line in lines[2::]:
line = re.sub(r'[^\w\s]', '', line)
nodes = line.split()
graph[nodes[0]] = [nodes[1], nodes[2]]
currentPosition = "AAA"
i = 0
while currentPosition != "ZZZ":
currentInstruction = instructions[i % lengthInstruction]
if currentInstruction == "L":
currentPosition = graph[currentPosition][0]
elif currentInstruction == "R":
currentPosition = graph[currentPosition][1]
i += 1
print("Total number of steps is: {}".format(i))
#Q2
startPositions = []
for key in graph.keys():
if key[-1] == 'A':
startPositions.append(key)
timeToReach = []
for currentPosition in startPositions:
i = 0
while currentPosition[-1] != 'Z':
currentInstruction = instructions[i % lengthInstruction]
if currentInstruction == "L":
currentPosition = graph[currentPosition][0]
elif currentInstruction == "R":
currentPosition = graph[currentPosition][1]
i += 1
timeToReach.append(i)
timeToReachAll = np.lcm.reduce(timeToReach)
print("Total number of ghost steps is: {}".format(timeToReachAll))