-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp2_new.py
131 lines (116 loc) · 2.8 KB
/
p2_new.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"""f = open('b1.in', 'r')
lines = f.readlines()
index = 0
num_tests = int(lines[index].strip())
index += 1 #move to blank line"""
last_call_ans = 0
ir = 43
def move_left(nums):
new_nums = [0]*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]*size_strip
inPair = False
pastNum = 0
index = len(new_nums)-1
nums.reverse()
pairs = enumerate(nums)
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
return new_nums
def random_shit():
global ms
global ir
global last_call_ans
c = 1 if ((ir>=43) and last_call_ans < 0) else 0
ans = (ms[ir-22] - ms[ir-43] - c) % (2**32)
last_call_ans = ans
ir += 1
return ans
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)
else:
new_nums = move_right(nums)
#get new random val
num_empty = sum([1 for x in nums if x == 0])
if new_nums == nums:
if num_empty==0:
new_nums = new_nums
else:
pos = random_shit() % num_empty
if (random_shit() % 10) == 0:
new_value = 4
else:
new_value = 2
#insert it
empty_counter = 0
for i,num in enumerate(new_nums):
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]"""