-
Notifications
You must be signed in to change notification settings - Fork 0
/
untitled
113 lines (101 loc) · 2.53 KB
/
untitled
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
104
105
106
107
108
109
110
111
112
"""f = open('b1.in', 'r')
lines = f.readlines()
index = 0
num_tests = int(lines[index].strip())
index += 1 #move to blank line"""
def move_left(nums):
new_nums = [0]*len(size_strip)
inPair = False
pastNum = 0
index = 0
for i, num in enumerate(nums):
if num == 0:
continue
else:
if inPair and pastNum == num:
new_nums[index] = pastNum*2
index += 1
elif inPair and pastNum != num:
new_nums[index] = pastNum
new_nums[index+1] = num
inPair = False
index += 2
elif not inPair:
pastNum = num
inPair = True
return new_nums
def move_right(nums):
new_nums = [0]*len(size_strip)
inPair = False
pastNum = 0
index = len(new_nums)-1
pairs = enumerate(nums)
pairs.reverse()
for i, num in pairs:
if num == 0:
continue
else:
if inPair and pastNum == num:
new_nums[index] = pastNum*2
index -= 1
elif inPair and pastNum != num:
new_nums[index] = pastNum
new_nums[index-1] = num
inPair = False
index -= 2
elif not inPair:
pastNum = num
inPair = True
with open('b1.in','r') as infile:
num_test_cases = int(infile.next().strip())
print num_test_cases
for i in range(0, num_test_cases):
infile.next()
size_strip = int(infile.next().strip())
print "size_strip", size_strip
nums = infile.next().strip().split()
print "nums", nums
ms = infile.next().strip().split()
ms = [int(m) for m in ms]
num_moves = int(infile.next().strip())
print "num_moves", num_moves
moves = list(infile.next().strip())
print "moves", moves
for move in moves:
if move == 'l':
new_nums = move_left(nums, size_strip)
else:
new_nums = move_right(nums, size_strip)
#get new random val
num_empty = sum([1 for x in nums if x == 0])
if new_nums == nums:
pos = random() % num_empty
if (random() % 10) == 0:
new_value = 4
else:
new_value = 2
#insert it
empty_counter = 0
for i,num in enumerate(new_num):
if new_nums[i] != 0:
continue
if new_nums[i] == 0 and empty_counter == pos:
new_nums[i] = new_value
else:
empty_counter += 1
nums = new_nums
"""while index < len(lines):
index += 1 #move past blank line
size_strip = int(lines[index].strip())
print size_strip
index += 1 #move past size strip line
nums = lines[index].strip().split()
nums = [int(num) for num in nums] #the numbers in the 2048 grid (row)
print nums
index += 1 #move past strip nums
ms = lines[index].strip().split()
ms = [int(m) for m in ms]
index += 1
num_moves = int(lines[index])
index += 1
moves = lines[index]"""