-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path#Altered states.py
292 lines (230 loc) · 8.56 KB
/
#Altered states.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#Altered states
#Make the 5x5 grid by getting the input
import numpy as np
from collections import Counter
import time
#list of states
state_populations = {
"california": 39538223,
"texas": 29145505,
"florida": 21538187,
"newyork": 20201249,
"pennsylvania": 13002700,
"illinois": 12812508,
"ohio": 11799448,
"georgia": 10711908,
"northcarolina": 10439388,
"michigan": 10077331,
"newjersey": 9288994,
"virginia": 8631393,
"washington": 7693612,
"arizona": 7151502,
"massachusetts": 7029917,
"tennessee": 6910840,
"indiana": 6785528,
"missouri": 6154913,
"maryland": 6177224,
"wisconsin": 5893718,
"colorado": 5773714,
"minnesota": 5706494,
"southcarolina": 5118425,
"alabama": 5024279,
"louisiana": 4657757,
"kentucky": 4505836,
"oregon": 4237256,
"oklahoma": 3959353,
"connecticut": 3605944,
"utah": 3271616,
"iowa": 3190369,
"nevada": 3104614,
"arkansas": 3011524,
"mississippi": 2961279,
"kansas": 2937880,
"newmexico": 2117522,
"nebraska": 1961504,
"westvirginia": 1793716,
"idaho": 1839106,
"hawaii": 1455271,
"maine": 1362359,
"newhampshire": 1377529,
"montana": 1084225,
"rhodeisland": 1097379,
"delaware": 989948,
"southdakota": 886667,
"northdakota": 779094,
"alaska": 733391,
"vermont": 643077,
"wyoming": 576851
}
def string_to_2d_array(s):
if len(s) != 25:
raise ValueError("The string must be exactly 25 characters long.")
# Create the 2D array
array_2d = []
for i in range(0, 25, 5):
row = list(s[i:i+5])
array_2d.append(row)
return array_2d
def all_or_all_but_one_letters_in_string(short_string, long_string):
short_counter = Counter(short_string)
long_counter = Counter(long_string)
missing_count = 0
for char, count in short_counter.items():
if long_counter[char] < 1: #if not in long string add one to count
missing_count += 1
if missing_count > 1:
return False
return True
#checks the
def possible_states_in_grid(grid):
states = []
for state in state_populations.keys():
if(all_or_all_but_one_letters_in_string(state, grid)):
states.append(state)
return states
def get_total_population(states):
sum = 0
for state in states:
sum += state_populations.get(state)
return sum
def states_in_grid(grid):
grid_matrix = string_to_2d_array(grid)
for state in possible_states_in_grid(grid):
return
# def state_in_grid(grid, state):
# i = 0
# route = []
# for coordinate in get_start_position(grid,list(state)[0]):
# print(coordinate)
# print(coordinate[0], coordinate[1])
# for c in range(1, len(state)):
# if king_move(grid, coordinate[0], coordinate[1], list(state)[i]) == False:
# return False
# route.append(coordinate)
# i += 1
# print("list state i:",list(state)[i] )
# print(king_move(grid, coordinate[0], coordinate[1], list(state)[i]))
# for cord in king_move(grid, coordinate[0], coordinate[1], list(state)[i]):
# if(coordinate == False):
# return False
# if i == len(state)-1:
# return route
# return False
def get_start_position(grid, target):
start_pos = []
for x in range(len(grid)):
for y in range(len(grid)):
if grid[x][y] == target:
start_pos.append([x,y])
return start_pos
def king_move(grid, x, y, target):
moves = []
for i in range(x-1,x+2):
for j in range(y-1,y+2):
if i>= 0 and i<len(grid) and j>=0 and j<len(grid) and (not (i==x and j ==y)):
if grid[i][j] == target or target == '5':
moves.append([i,j])
return moves
def cords_not_visited(counter, grid_size):
list = []
for x in range(grid_size):
for y in range(grid_size):
if tuple([x,y]) not in counter:
list.append(tuple[x,y])
return list
def recursiontest(grid, state, start_coordinate, i, start_string, joker):
if start_string == state:
cords_seen.append(tuple(start_coordinate))
return True
else:
#print("looking for: ", state, start_coordinate, start_string)
if (not king_move(grid, start_coordinate[0], start_coordinate[1], state[i])) == False:
start_string = start_string + state[i]
for cord in king_move(grid, start_coordinate[0], start_coordinate[1], state[i]):
if recursiontest(grid,state,cord,i+1, start_string, joker):
cords_seen.append(tuple(cord))
return True
else:
if joker:
start_string = start_string + state[i]
for cord in king_move(grid, start_coordinate[0], start_coordinate[1], '5'):
if recursiontest(grid,state,cord,i+1, start_string, False):
cords_seen.append(tuple(cord))
return True
if joker:
for cord in king_move(grid, start_coordinate[0], start_coordinate[1], '5'):
if recursiontest(grid,state,cord,i+1, start_string, False):
cords_seen.append(tuple(cord))
return True
def missing_states(list_states):
missing_states = {}
for state, population in state_populations.items():
if state not in list_states:
missing_states.update({state: population})
return sorted(missing_states.items(), key=lambda item: item[1], reverse=True)
def find_all_states(grid):
possible_states = possible_states_in_grid(grid)
grid = string_to_2d_array(grid)
all_states = []
for state in possible_states:
if not get_start_position(grid, state[0]):
state_alt = state[1:]
for coords in get_start_position(grid, state_alt[0]):
if recursiontest(grid, state_alt, coords,1, state_alt[0], False):
cords_seen.append(tuple(coords))
all_states.append(state)
break
for coords in get_start_position(grid, state[0]):
if recursiontest(grid, state, coords,1, state[0], True):
all_states.append(state)
cords_seen.append(tuple(coords))
break
state_alt = state[1:]
if state not in all_states:
for coords in get_start_position(grid, state_alt[0]):
if recursiontest(grid, state_alt, coords,1, state_alt[0], False):
cords_seen.append(tuple(coords))
all_states.append(state)
break
return all_states
def print_output(grid):
print("--------------")
print("score", get_total_population(find_all_states(grid)), find_all_states(grid))
print("-------------")
print("largest missing states:", missing_states(find_all_states(grid)))
def joker(grid, state):
altered_state = []
for c in list(state):
return
def replace_chars_with_x(s):
result = []
for i in range(len(s)):
# Replace the i-th character with 'x'
new_string = s[:i] + 'x' + s[i+1:]
result.append(new_string)
return result
st = 'aaska'
start_time = time.time()
cords_seen = []
# print("--- %s seconds ---" % (time.time() - start_time))
# print("score", get_total_population(find_all_states('akweolyhmnaiodaxefritlogn')), find_all_states('akweolyhmnaiodaxefritlogn'))
# counter = {}
# print("largest missing states:", missing_states(find_all_states('akweolyhmnaiodaxefritlogn')))
#print("test the recursion:", recursiontest(string_to_2d_array(), 'alaska',[0,0],1, 'a'))
print_output("marogntseiasewalinyhforku")
counter = {}
list_cords = []
for x in range(5):
for y in range(5):
list_cords.append(tuple([x,y]))
for cord in cords_seen:
if cord not in counter:
counter[cord] = 0
counter[cord] += 1
print(counter)
for x in range(5):
for y in range(5):
if tuple([x,y]) in cords_seen:
if counter[tuple([x,y])] == 3:
print([x,y])
# getest: marogntseiasejalinwdforka, clfrgaionikrsnadweaeayetx