Finished on 2023-12-08 p2
This commit is contained in:
parent
06fcf5f80c
commit
953be2f650
@ -5,6 +5,7 @@ import sys
|
|||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
from collections import Counter
|
from collections import Counter
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
from math import lcm
|
||||||
|
|
||||||
input_f = sys.argv[1]
|
input_f = sys.argv[1]
|
||||||
|
|
||||||
@ -12,53 +13,31 @@ def pp(x):
|
|||||||
for i in x:
|
for i in x:
|
||||||
print(nodes[i].value,nodes[i].left,nodes[i].right)
|
print(nodes[i].value,nodes[i].left,nodes[i].right)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
d, *maps = open(input_f).read().split('\n')
|
d, *maps = open(input_f).read().split('\n')
|
||||||
|
|
||||||
|
|
||||||
class Node():
|
class Node():
|
||||||
def __init__(self,value,left,right):
|
def __init__(self,value,left,right):
|
||||||
self.value = value
|
self.value = value
|
||||||
self.left = left
|
self.left = left
|
||||||
self.right = right
|
self.right = right
|
||||||
|
|
||||||
s = []
|
|
||||||
l = []
|
|
||||||
r = []
|
|
||||||
|
|
||||||
maps = maps
|
|
||||||
|
|
||||||
nodes = {}
|
nodes = {}
|
||||||
|
|
||||||
for i in maps:
|
for i in maps:
|
||||||
if i == '':
|
if i == '':
|
||||||
continue
|
continue
|
||||||
s = i[:3]
|
nodes[i[:3]] = Node(i[:3],i[7:10],i[12:15])
|
||||||
l = i[7:10]
|
|
||||||
r = i[12:15]
|
|
||||||
nodes[s] = Node(s,l,r)
|
|
||||||
|
|
||||||
current = 'AAA'
|
current = 'AAA'
|
||||||
steps = 0
|
steps = 0
|
||||||
count = 0
|
count = 0
|
||||||
try:
|
while current != 'ZZZ':
|
||||||
while current != 'ZZZ':
|
|
||||||
steps += 1
|
steps += 1
|
||||||
current = nodes[current].left if d[count%len(d)] == 'L' else nodes[current].right
|
current = nodes[current].left if d[count%len(d)] == 'L' else nodes[current].right
|
||||||
count += 1
|
count += 1
|
||||||
print(steps)
|
print('Part 1: ',end='')
|
||||||
except:
|
print(steps)
|
||||||
print()
|
|
||||||
|
|
||||||
def done(x):
|
|
||||||
print(x)
|
|
||||||
for i in x:
|
|
||||||
if not i.endswith('Z'):
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
pp(nodes)
|
|
||||||
|
|
||||||
current = []
|
current = []
|
||||||
for i in nodes:
|
for i in nodes:
|
||||||
@ -66,15 +45,14 @@ for i in nodes:
|
|||||||
current.append(i)
|
current.append(i)
|
||||||
count = 0
|
count = 0
|
||||||
steps = 0
|
steps = 0
|
||||||
while not done(current):
|
total_s = []
|
||||||
|
for i in current:
|
||||||
|
count = 0
|
||||||
|
steps = 0
|
||||||
|
while not i.endswith('Z'):
|
||||||
steps += 1
|
steps += 1
|
||||||
for i in range(len(current)):
|
i = nodes[i].left if d[count%len(d)] == 'L' else nodes[i].right
|
||||||
if d[count%len(d)] == 'L':
|
|
||||||
#print("Going left from " + str(current[i]) + " to " + str(nodes[current[i]].left))
|
|
||||||
current[i] = nodes[current[i]].left
|
|
||||||
else:
|
|
||||||
#print("Going right from " + str(current[i]) + " to " + str(nodes[current[i]].right))
|
|
||||||
current[i] = nodes[current[i]].right
|
|
||||||
count += 1
|
count += 1
|
||||||
print(steps)
|
total_s.append(steps)
|
||||||
|
print("Part 2: ",end='')
|
||||||
|
print(lcm(*total_s))
|
||||||
|
Loading…
Reference in New Issue
Block a user