add: part 2
This commit is contained in:
		
							
								
								
									
										36
									
								
								2023/day08/src/part2.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								2023/day08/src/part2.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,36 @@
 | 
			
		||||
import math
 | 
			
		||||
 | 
			
		||||
text: str = open("input.txt", "r").read()
 | 
			
		||||
 | 
			
		||||
_value: int = 0 
 | 
			
		||||
 | 
			
		||||
lines = text.splitlines()
 | 
			
		||||
 | 
			
		||||
deplacements = lines[0]
 | 
			
		||||
ground = {}
 | 
			
		||||
 | 
			
		||||
starts = []
 | 
			
		||||
for line in lines[2:]:
 | 
			
		||||
	data = line.split(" = ")
 | 
			
		||||
	if (data[0][2] == 'A'):
 | 
			
		||||
		starts.append(data[0])
 | 
			
		||||
	data[1] = data[1][1:9]
 | 
			
		||||
	ground.update({data[0]: data[1].split(", ")})
 | 
			
		||||
 | 
			
		||||
def resolve(ground: dict, deplacements: str, start: str):
 | 
			
		||||
	nb_deplacement = 0
 | 
			
		||||
	pos = start
 | 
			
		||||
	while True:
 | 
			
		||||
		for deplacement in deplacements:
 | 
			
		||||
			if (pos[2] == "Z"):
 | 
			
		||||
				return (nb_deplacement)
 | 
			
		||||
			nb_deplacement += 1
 | 
			
		||||
			pos = ground.get(pos)[deplacement == "R"]
 | 
			
		||||
 | 
			
		||||
lst = []
 | 
			
		||||
for start in starts:
 | 
			
		||||
	lst.append(resolve(ground, deplacements, start))
 | 
			
		||||
 | 
			
		||||
_value = math.lcm(*lst)
 | 
			
		||||
 | 
			
		||||
print(_value)
 | 
			
		||||
		Reference in New Issue
	
	Block a user