day 8, somewhat easy
part 1 realy easy part 2 made with a trick, got from reddit megathread else naive approach would have taken 40 day of computation
This commit is contained in:
		
							
								
								
									
										24
									
								
								d08/part1.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								d08/part1.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,24 @@
 | 
			
		||||
import sys
 | 
			
		||||
from itertools import cycle
 | 
			
		||||
 | 
			
		||||
L = [l for l in sys.stdin.read().splitlines()]
 | 
			
		||||
 | 
			
		||||
I = L[0]  # instructions
 | 
			
		||||
 | 
			
		||||
for l in L[2:]:
 | 
			
		||||
    print(f'%{l}%')
 | 
			
		||||
 | 
			
		||||
Q = [l.split(' = ') for l in L[2:] ]
 | 
			
		||||
P = {k:v[1:-1].split(", ") for [k,v] in Q }
 | 
			
		||||
 | 
			
		||||
print(P)
 | 
			
		||||
 | 
			
		||||
C = 'AAA'
 | 
			
		||||
N = 0
 | 
			
		||||
for i in cycle(I):
 | 
			
		||||
    if C == 'ZZZ':
 | 
			
		||||
        break
 | 
			
		||||
    C = P[C][i == 'R']
 | 
			
		||||
    N += 1
 | 
			
		||||
print(C)
 | 
			
		||||
print(N)
 | 
			
		||||
							
								
								
									
										22
									
								
								d08/part2.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								d08/part2.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()]
 | 
			
		||||
Q = [l.split(' = ') for l in P ]
 | 
			
		||||
P = {k:v[1:-1].split(", ") for [k,v] in Q }
 | 
			
		||||
 | 
			
		||||
G = [p for p in P.keys() if p[2] == 'A' ]
 | 
			
		||||
R = []
 | 
			
		||||
for n,g in enumerate(G):
 | 
			
		||||
    print(n+1, g, '', end='')
 | 
			
		||||
    C = g
 | 
			
		||||
    N = 0
 | 
			
		||||
    for i in cycle(I):
 | 
			
		||||
        if C[2] == 'Z':
 | 
			
		||||
            break
 | 
			
		||||
        C = P[C][i == 'R']
 | 
			
		||||
        N += 1
 | 
			
		||||
    print(N)
 | 
			
		||||
    R.append(N)
 | 
			
		||||
print('steps: sum:', sum(R), 'lcm:', lcm(*R))
 | 
			
		||||
		Reference in New Issue
	
	Block a user