2023 - Day 8 part 2
This commit is contained in:
parent
2a004b9110
commit
7ab3e3ec5a
2 changed files with 65 additions and 0 deletions
55
2023/8/8_2.py
Normal file
55
2023/8/8_2.py
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
# 2023 - Advent Of Code 8 - part 2
|
||||||
|
|
||||||
|
from math import lcm
|
||||||
|
from functools import reduce
|
||||||
|
|
||||||
|
#file = 'input_example2.txt'
|
||||||
|
file = 'input.txt'
|
||||||
|
|
||||||
|
|
||||||
|
# pylint: disable=consider-using-with
|
||||||
|
input_lines = [line.strip('\n') for line in open(file, encoding="utf-8")]
|
||||||
|
|
||||||
|
directions = input_lines[0]
|
||||||
|
print(directions)
|
||||||
|
|
||||||
|
nodes = {}
|
||||||
|
for l in input_lines[2:]:
|
||||||
|
k, v = l.split(' = ')
|
||||||
|
nodes[k] = v[1:-1].split(', ')
|
||||||
|
|
||||||
|
cur_nodes = [x for x in nodes if x.endswith('A')]
|
||||||
|
print(cur_nodes)
|
||||||
|
|
||||||
|
allz = False
|
||||||
|
count = 0
|
||||||
|
ends_num = [0] * len(cur_nodes)
|
||||||
|
while not allz:
|
||||||
|
ends = [False] * len(cur_nodes)
|
||||||
|
for current, _ in enumerate(cur_nodes):
|
||||||
|
# print(cur_node)
|
||||||
|
|
||||||
|
if directions[count % len(directions)] == 'L':
|
||||||
|
cur_nodes[current] = nodes[cur_nodes[current]][0]
|
||||||
|
elif directions[count % len(directions)] == 'R':
|
||||||
|
cur_nodes[current] = nodes[cur_nodes[current]][1]
|
||||||
|
# check z
|
||||||
|
if cur_nodes[current].endswith('Z'):
|
||||||
|
ends[current] = True
|
||||||
|
ends_num[current] = count
|
||||||
|
print(f'end {cur_nodes[current]} found at {count} for {current} {ends}')
|
||||||
|
allz = False not in ends
|
||||||
|
if True in ends:
|
||||||
|
# print(f'end found at {count} {ends}')
|
||||||
|
print(f'ends list: {ends_num}')
|
||||||
|
if 0 not in ends_num:
|
||||||
|
break
|
||||||
|
count += 1
|
||||||
|
|
||||||
|
print()
|
||||||
|
print(f'count: {count} {ends}')
|
||||||
|
print(f'{ends_num}')
|
||||||
|
ends_num2 = [x + 1 for x in ends_num]
|
||||||
|
print(f'reduced: {reduce(lcm, ends_num)}')
|
||||||
|
print(f'reduced: {reduce(lcm, ends_num2)}')
|
10
2023/8/input_example2.txt
Normal file
10
2023/8/input_example2.txt
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
LR
|
||||||
|
|
||||||
|
11A = (11B, XXX)
|
||||||
|
11B = (XXX, 11Z)
|
||||||
|
11Z = (11B, XXX)
|
||||||
|
22A = (22B, XXX)
|
||||||
|
22B = (22C, 22C)
|
||||||
|
22C = (22Z, 22Z)
|
||||||
|
22Z = (22B, 22B)
|
||||||
|
XXX = (XXX, XXX)
|
Loading…
Reference in a new issue