/
revenge.py
65 lines (55 loc) · 1.83 KB
/
revenge.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
57
58
59
60
61
62
63
64
65
import utils
def process(input):
number_of_cases = int(input[0][0])
cases = [row for row in input[1:]]
solution = [solve(case, cases[case])
for case in range(0, number_of_cases)]
return solution
def solve(case, situation):
print("Solving: " + str(case))
stack = [char for char in situation[0]]
flag = True
i = 0
if stack.count('-') == 0:
return "Case #"+str(case + 1)+": " + str(0)
while flag:
result = xray(stack)
i += 1
if result.count('-') == 0:
flag = False
stack = result
return "Case #"+str(case + 1)+": " + str(i)
def xray(stack):
revStack = list(reversed(stack))
for pancake in range(0, len(stack)):
if revStack[pancake] == '-':
if revStack[len(stack) - 1] == '+':
index = revStack.index('+', pancake)
toFlip = revStack[index:]
stayTheSame = revStack[:index]
elif pancake == len(stack) - 2:
if revStack[pancake + 1] == '-':
toFlip = revStack[pancake:]
stayTheSame = revStack[:pancake]
else:
toFlip = revStack[pancake + 1:]
stayTheSame = revStack[:pancake+1]
else:
toFlip = revStack[pancake:]
stayTheSame = revStack[:pancake]
flipped = flip(toFlip)
stayTheSame.extend(flipped)
return list(reversed(stayTheSame))
def flip(arr):
rev = list(reversed(arr))
ans = []
for index in range(0, len(rev)):
if rev[index] == '-':
ans.append('+')
elif rev[index] == '+':
ans.append('-')
return ans
filename = utils.getFilename()
input = utils.read_input(filename)
output = process(input)
utils.print_output(filename, output)