day 8, generators are faster (2x)
This commit is contained in:
		
							
								
								
									
										22
									
								
								d08/run.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								d08/run.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,22 @@
 | 
				
			|||||||
 | 
					import sys
 | 
				
			||||||
 | 
					from itertools import cycle
 | 
				
			||||||
 | 
					from math import lcm
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					I, _, *P = [l for l in sys.stdin.read().splitlines()]
 | 
				
			||||||
 | 
					# 0000000000111111111
 | 
				
			||||||
 | 
					# 0123456789012345678
 | 
				
			||||||
 | 
					# BCN = (HFN, KFC)
 | 
				
			||||||
 | 
					P = {l[0:3]:(l[7:10],l[12:15]) for l in P}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def countsteps(g):
 | 
				
			||||||
 | 
					    C = g
 | 
				
			||||||
 | 
					    N = 0
 | 
				
			||||||
 | 
					    for i in cycle(I):
 | 
				
			||||||
 | 
					        if C[2] == 'Z':
 | 
				
			||||||
 | 
					            break
 | 
				
			||||||
 | 
					        C = P[C][i == 'R']
 | 
				
			||||||
 | 
					        N += 1
 | 
				
			||||||
 | 
					    return N
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					print("part 1:", countsteps("AAA"))
 | 
				
			||||||
 | 
					print("part 2:", lcm(*(countsteps(p) for p in P.keys() if p[2] == 'A')))
 | 
				
			||||||
		Reference in New Issue
	
	Block a user