-
Notifications
You must be signed in to change notification settings - Fork 0
/
day8.py
56 lines (49 loc) · 1.35 KB
/
day8.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
def read_file(path):
moves = ""
map = {}
with open(path, 'r') as file:
moves = file.readline().strip()
c = 0
for line in file:
if c == 0:
c += 1
continue
if line:
key, value = line.split("=")
key = key.strip()
values = list(val.strip() for val in value[1:-1].split(','))
values[0] = values[0][1:]
values[1] = values[1][:-1]
map[key] = values
return moves, map
def part_one(map, moves):
cur = 'AAA'
target = 'ZZZ'
res = 0
while cur != target:
for move in moves:
cur = map[cur][0] if move == 'L' else map[cur][1]
res += 1
return res
import math
def starting_points(map, moves):
start = []
for key in map.keys():
if key[-1] == 'A':
start.append(key)
return start
def part_two(map, moves):
def first_z(cur):
ans = 0
while (cur[-1] != 'Z'):
move = moves[ans % len(moves)]
cur = map[cur][0] if move == 'L' else map[cur][1]
ans += 1
return ans
start = starting_points(map, moves)
res = 1
for elt in start:
res = math.lcm(res, first_z(elt))
return res
moves, map = read_file("input88.txt")
print(part_two(map,moves))