-
Notifications
You must be signed in to change notification settings - Fork 1
/
merge_dragon.py
executable file
·190 lines (152 loc) · 4.6 KB
/
merge_dragon.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env python3
## Lets check how much merge is needed for different levels
## in dragon merge.
## lets check 3 merge line and 5 merge line,
## and see like, how long it takes to reach greater levels
## Sample run output.
## Fuck merge dragon. I need to merge whole life to create wonders.
'''
$ python3 merge_dragon.py
Choose max level as 18
Type: MERGE_BY_3
max_level: 18
tot_merges: 64570081
tot_wanted: 129140163
max_squares: 35
Units Left:
Level 18: 1
Type: MERGE_BY_5
max_level: 18
tot_merges: 4979685
tot_wanted: 14939065
max_squares: 62
Units Left:
Level 18: 2
Level 17: 1
Level 16: 1
Level 12: 1
Level 11: 1
Level 10: 1
Level 8: 1
Level 6: 1
Level 2: 1
Type: HYBRID_MERGE
max_level: 18
tot_merges: 2489847 (merge_by_3-9, merge_by_5-2489838)
tot_wanted: 7469533
max_squares: 0
Units Left:
Level 18: 1
'''
import sys
# Max level we could calculate by default
MAX_LEVEL = 18
MERGE_BY_3 = 3
MERGE_BY_5 = 5
def calc_merge_3(max_level):
return calc_merge (max_level, MERGE_BY_3)
def calc_merge_5(max_level):
return calc_merge (max_level, MERGE_BY_5)
## Handles MERGE_BY_3 and MERGE_BY_5
def calc_merge (max_level, min_merge):
merge_dict = {}
merge_dict['type'] = 'MERGE_BY_{}'.format(min_merge)
merge_dict['merge'] = {}
for i in range(1, max_level+1):
merge_dict['merge'][i] = 0
current_high_level = 0
tot_merges = 0
squares = 0
max_squares = 0
tot_wanted = 0
merge_tree = merge_dict['merge']
while (merge_tree[max_level] == 0):
merge_tree[1] += min_merge
tot_wanted += min_merge
squares += min_merge
if (squares > max_squares):
max_squares = squares
for i in range (1, max_level):
new = (merge_tree[i] // min_merge)
if new == 0:
break
if (min_merge == MERGE_BY_3):
## incase of merge by 3, we get one new units
merge_tree[i+1] += new
## when we merge 3 units, the new object takes 1 unit space.
## so -3 + 1 = -2
squares -= (2*new)
elif (min_merge == MERGE_BY_5):
## incase of merge by 5, we get two new units
merge_tree[i+1] += new*2
## when we merge 5 units, the new object takes 2 unit space.
## so -5 + 2 = -3
squares -= (3*new)
merge_tree[i] = merge_tree[i] % min_merge
tot_merges += 1
merge_dict['max_level'] = max_level
merge_dict['tot_merges'] = tot_merges
merge_dict['tot_wanted'] = tot_wanted
merge_dict['max_squares'] = max_squares
return merge_dict
## Calculates merge by 5 or 3,
## we dont want 2 units of final level
def calc_merge_hybrid(max_level):
merge_dict = {}
merge_dict['type'] = 'HYBRID_MERGE'
merge_dict['max_level'] = max_level
merge_dict['merge'] = {}
for i in range(1, max_level+1):
merge_dict['merge'][i] = 0
merge_tree = merge_dict['merge']
merge_tree[max_level] = 1
tot_merge_by_5 = 0
tot_merge_by_3 = 0
for level in range(max_level, 1, -1):
merge_by_5 = merge_tree[level] // 2
merge_by_3 = merge_tree[level] % 2
merge_tree[level] = 0
merge_tree[level-1] = (merge_by_5 * 5) + (merge_by_3 * 3)
tot_merge_by_5 += merge_by_5
tot_merge_by_3 += merge_by_3
merge_dict['tot_merges'] = tot_merge_by_5 + tot_merge_by_3
merge_dict['tot_merges_by_3'] = tot_merge_by_3
merge_dict['tot_merges_by_5'] = tot_merge_by_5
merge_dict['tot_wanted'] = merge_tree[1]
## ToDo: To be handled later
merge_dict['max_squares'] = 0
## reset the tree
merge_tree[1] = 0
merge_tree[max_level] = 1
return merge_dict
def print_merge (merge_dict):
if (merge_dict['type'] == 'HYBRID_MERGE'):
print ((' Type: {}\n max_level: {}\n' +
' tot_merges: {} (merge_by_3-{}, merge_by_5-{})\n' +
' tot_wanted: {}\n max_squares: {}').
format(merge_dict['type'], merge_dict['max_level'],
merge_dict['tot_merges'],
merge_dict['tot_merges_by_3'],
merge_dict['tot_merges_by_5'],
merge_dict['tot_wanted'],
merge_dict['max_squares']))
else:
print ((' Type: {}\n max_level: {}\n tot_merges: {}\n' +
' tot_wanted: {}\n max_squares: {}').
format(merge_dict['type'], merge_dict['max_level'],
merge_dict['tot_merges'], merge_dict['tot_wanted'],
merge_dict['max_squares']))
print (' Units Left:')
for i in range(merge_dict['max_level'], 0, -1):
if (merge_dict['merge'][i] == 0):
continue
print (' Level {:2}: {}'.format(i, merge_dict['merge'][i]))
print ('\n')
if __name__ == "__main__":
if (len(sys.argv) <= 1) or (int(sys.argv[1]) < 2):
max_level = MAX_LEVEL
print (' Choose max level as {}'.format(MAX_LEVEL))
else:
max_level = int(sys.argv[1])
for merge_handle in [calc_merge_3, calc_merge_5, calc_merge_hybrid]:
print_merge (merge_handle(max_level))