-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.py
53 lines (41 loc) · 1.13 KB
/
solution.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
input = [x for x in open("data.txt","r").read().split(",")]
dancefloor = []
def InitDancefloor():
return [chr(l) for l in range(97, 113)]
def DanceStep(x, df):
if x[0]=="s":
s = int(x[1:])
df = df[-s:] + df[0:-s]
if x[0]=="x":
s = [int(z) for z in x[1:].split("/")]
tmp = df[s[0]]
df[s[0]] = df[s[1]]
df[s[1]] = tmp
if x[0]=="p":
s = [df.index(z) for z in x[1:].split("/")]
tmp = df[s[0]]
df[s[0]] = df[s[1]]
df[s[1]] = tmp
return df
def Dance(steps, df):
for x in steps:
df = DanceStep(x, df)
return df
def DetectPeriod(steps):
df = InitDancefloor()
l = []
df = Dance(steps, df)
while True:
df = Dance(steps, df)
if tuple(df) in l: break
l.append(tuple(df))
return l.__len__()
dancefloor = InitDancefloor()
#First part
dancefloor = Dance(input, dancefloor)
print("First part: " + "".join(dancefloor))
# Second part
MagicNumber = DetectPeriod(input)
for i in range((1000000000-1) % MagicNumber):
dancefloor = Dance(input, dancefloor)
print("Second part: " + "".join(dancefloor))