-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday8.py
More file actions
53 lines (51 loc) · 1.35 KB
/
day8.py
File metadata and controls
53 lines (51 loc) · 1.35 KB
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
from collections import defaultdict
from math import lcm
def star1():
inp = contents[0]
steps = 0
values = defaultdict(list)
for line in contents[2:]:
line = line.split(" = ")
lst = line[1][1:-1].split(', ')
values[line[0]] = lst
place = ["AAA"]
while not(all('Z' in str(x) for x in place)):
for i in inp:
if i == "R":
move = 1
else:
move = 0
for i, p in enumerate(place):
place[i] = values[p][move]
steps += 1
if all('Z' in str(x) for x in place):
break
return steps
def star2():
inp = contents[0]
place = []
values = defaultdict(list)
for line in contents[2:]:
line = line.split(" = ")
lst = line[1][1:-1].split(', ')
if "A" in line[0]:
place.append(line[0])
values[line[0]] = lst
lcms = []
for p in place:
steps = 0
while "Z" not in p:
for i in inp:
if i == "R":
move = 1
else:
move = 0
p = values[p][move]
steps += 1
if "Z" in p:
break
lcms.append(steps)
return lcm(*lcms)
contents = open("day8.txt").read().splitlines()
print(star1())
print(star2())