Last active
November 16, 2018 06:56
-
-
Save wojons/ca21c8f21946d9bc24c6b03a07c35608 to your computer and use it in GitHub Desktop.
touching-tick-tack-idk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
#generate a grid | |
grid_width = 10 | |
grid_height = 10 | |
grid_data = dict() | |
print_cache = dict() | |
conneceted_dict = dict() | |
total_grid_size = grid_height * grid_width | |
#pick a random number between 2 and the number of grids cells | |
total_filled_cells = random.randint(2, total_grid_size) | |
filled_cell_list = random.sample(range(total_grid_size), total_filled_cells) | |
#display the grid for us to look at | |
cell_num = 0 | |
for y in range(grid_height): | |
grid_data[y] = dict() | |
for x in range(grid_width): | |
if cell_num in filled_cell_list: | |
grid_data[y][x] = True | |
print("x", end =" "), | |
else: | |
grid_data[y][x] = False | |
print("o", end =" "), | |
cell_num += 1 | |
print("") | |
#show the data struct that we have | |
#print(grid_data) | |
#find all the connected grids | |
test_cell_num = 0 | |
for y in range(grid_height): | |
for x in range(grid_width): | |
#only test the points we already care about | |
if grid_data.get(y).get(x) is False: | |
test_cell_num += 1 | |
continue | |
my_loc = [y, x] | |
my_loc_str = '{},{}'.format(y, x) | |
# make a list of who to test | |
test_list = list() | |
if x > 0: # add test for to the left | |
test_list.append([y, x-1]) | |
if x < grid_width-1: # add test for to the right | |
test_list.append([y, x+1]) | |
if y > 0: # add test for above | |
test_list.append([y-1, x]) | |
if y < grid_height-1: # add test for below | |
test_list.append([y+1, x]) | |
#print the points we want to test | |
#print("{} -> {}".format(my_loc, test_list)) | |
for test_point in test_list: | |
test_point_loc_str = '{},{}'.format(test_point[0], test_point[1]) | |
if print_cache.get('{},{} -> {},{}'.format(y, x, test_point[0], test_point[1])) is None and grid_data.get(test_point[0]).get(test_point[1]) is True: | |
#use this store a list of cells that need to be marked as touching | |
temp_conneceted_track = list() | |
temp_track_dirty = False | |
#we are going to make a nice map that tracks and computes all the cells that are conneceted to one another to make it easier then having to do path | |
#lookups later on when we are asked the question | |
if conneceted_dict.get(my_loc_str) is None: | |
conneceted_dict[my_loc_str] = dict() | |
if conneceted_dict.get(test_point_loc_str) is None: | |
conneceted_dict[test_point_loc_str] = dict() | |
#add these two points to be updated to mark each other as being conneceted | |
temp_conneceted_track.append(my_loc_str) | |
temp_conneceted_track.append(test_point_loc_str) | |
temp_track_dirty = True | |
while temp_track_dirty is True: | |
#mark as false for now since we are processing the list | |
temp_track_dirty = False | |
for track_cell in temp_conneceted_track: | |
cell_curr_keys = list(conneceted_dict[track_cell]) | |
cell_new_keys = list(set(cell_curr_keys) | set(temp_conneceted_track)) # get the keys we want to merge and the current keys merge and remove dups | |
#print(len(list(set(cell_curr_keys) - set(cell_new_keys)))) | |
#print(cell_curr_keys) | |
#print(set(cell_curr_keys)) | |
#print(set(cell_new_keys)) | |
#print("{} ++ {}".format(cell_curr_keys, cell_new_keys)) | |
if len(cell_curr_keys) == 0 or len(list(set(cell_curr_keys) ^ set(cell_new_keys))) > 0: | |
#loop over and make sure we set all of these as true | |
for cell_new_key in cell_new_keys: | |
#print("{} -- {}".format(track_cell, cell_new_key)) | |
conneceted_dict[track_cell][cell_new_key] = True | |
# mark everything as dirty and update the full list that needs to be loooped over again | |
temp_track_dirty = True | |
temp_conneceted_track = cell_new_keys.copy() | |
#print(temp_conneceted_track) | |
#write the current direction and the alt direction to the print cache so we dont see it again we probly dont need current direction just tossed it in | |
#print_cache['{},{} -> {},{}'.format(test_point[0], test_point[1], y, x)] = True | |
#print_cache['{},{} -> {},{}'.format(y, x, test_point[0], test_point[1])] = True | |
#print("{} is touching {}".format(my_loc, test_point)) | |
test_cell_num += 1 | |
#print(print_cache) | |
#print(conneceted_dict) | |
#check to see if the points are touching one another | |
def is_conneceted(st_point, nd_point): | |
global conneceted_dict | |
return conneceted_dict.get(st_point, dict()).get(nd_point, False) | |
while True: | |
print('Enter the first point (y,x): ', end =" ") | |
first_point = input() | |
print('Enter the second point (y,x): ', end =" ") | |
second_point = input() | |
if is_conneceted(first_point, second_point) is True: | |
print("The points {} and {} are connected".format(first_point, second_point)) | |
else: | |
print("The points {} and {} are not connected".format(first_point, second_point)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
x x x o o o x o o x | |
o o x x x o o o x o | |
o o o o x o o o x o | |
x x o x o o x o o o | |
o o o o x o x x o x | |
x o o o x x o o x o | |
x o o x x x x x o x | |
o o o x x o o o o o | |
x o o x x o x o o o | |
o o x o x o x x o x | |
Enter the first point (y,x): 8,6 | |
Enter the second point (y,x): 9,7 | |
The points 8,6 and 9,7 are connected | |
Enter the first point (y,x): 4,4 | |
Enter the second point (y,x): 6,7 | |
The points 4,4 and 6,7 are connected | |
Enter the first point (y,x): 4,4 | |
Enter the second point (y,x): 6,6 | |
The points 4,4 and 6,6 are connected | |
Enter the first point (y,x): 0,0 | |
Enter the second point (y,x): 9,9 | |
The points 0,0 and 9,9 are not connected |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment