-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem22_2.py
103 lines (83 loc) · 2.38 KB
/
problem22_2.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
""" Pylint told me I should add an doc String"""
import csv
import sys
import copy
MOVES = ['L','R','S']
def find_adjacent( var_frontier ):
var_moves = []
for var_m in MOVES:
var_v = valid_move( var_frontier, var_m)
if var_v is not ():
var_moves.append( var_v)
return var_moves
def valid_move( var_frontier , var_m):
if var_m == 'L':
return left(var_frontier)
elif var_m == 'R':
return right(var_frontier)
elif var_m == 'S':
return suck(var_frontier)
def check_config( var_i ):
return sum( var_i[:-1]) == 0
def left( var_frontier ):
var_config, var_move = var_frontier[-1]
var_f = [ f for f, m in var_frontier ]
var_c = copy.copy(var_config)
var_c[-1] -= 1
if inBounds( var_c ):
if var_c not in var_f:
return ( var_c, 'L' )
return ()
def right( var_frontier ):
var_config, var_move = var_frontier[-1]
var_f = [ f for f, m in var_frontier ]
var_c = copy.copy(var_config)
var_c[-1] += 1
if inBounds( var_c ):
if var_c not in var_f:
return ( var_c, 'R')
return ()
def suck( var_frontier ):
var_config, var_move = var_frontier[-1]
var_c = copy.copy(var_config)
if var_c[var_c[-1]] is 1:
var_c[var_c[-1]] -= 1
return (var_c, 'S')
else:
return ()
def inBounds( var_c ):
return var_c[-1] in range(0, len(var_c)-1)
def sanitize_input( var_i ):
if inBounds( var_i ) is False:
return False
if all( 0 <= i <= 1 for i in var_i[:-1]) is False:
return False
return True
def find_path( var_c ):
return bfs( var_c )
def bfs( var_c ):
queue = []
queue.append( [(list(var_c), "" ) ])
while queue:
path = queue.pop()
node_c, node_m = path[-1]
if check_config(node_c):
return [ m for _, m in path ]
adj = find_adjacent( path )
if not adj and not queue:
return list("None")
else:
for a in adj:
p = list( path )
p.append( a )
queue.insert( 0, p )
def main():
var_l = []
for line in csv.reader(sys.stdin):
var_l.append(map( lambda x: int(x), line ))
if sanitize_input(var_l[0]) is False:
print "invalid input"
sys.exit(0)
print "".join(find_path( var_l[0] ))
if __name__ == "__main__":
main()