Skip to content

Instantly share code, notes, and snippets.

@adammhaile
Created August 17, 2018 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adammhaile/857acdb331142f3564a5935ec7b808b6 to your computer and use it in GitHub Desktop.
Save adammhaile/857acdb331142f3564a5935ec7b808b6 to your computer and use it in GitHub Desktop.
from random import randint
import time
class Jewels:
def __init__(self):
self.matrix = [[randint(0, 5) for x in range(16)] for y in range(16)]
self._reset_visited()
def _reset_visited(self):
self.__visited = [[False for x in range(16)] for y in range(16)]
def print(self):
for row in self.matrix:
print(row)
def get(self, x, y):
return self.matrix[y][x]
def _find_group(self, x, y):
if self.__visited[y][x]:
return []
res = [(x, y)]
self.__visited[y][x] = True
val = self.get(x, y)
if x < 15 and self.get(x + 1, y) == val:
res.extend(self._find_group(x+1, y))
if x > 0 and self.get(x - 1, y) == val:
res.extend(self._find_group(x - 1, y))
if y < 15 and self.get(x, y + 1) == val:
res.extend(self._find_group(x, y + 1))
if y > 0 and self.get(x, y - 1) == val:
res.extend(self._find_group(x, y - 1))
return res
def find_groups(self):
self._reset_visited()
groups = []
try:
for x in range(16):
for y in range(16):
group = self._find_group(x, y)
if len(group) >= 3:
groups.append(group)
except:
raise
return groups
j = Jewels()
j.print()
start = time.time()
groups =j.find_groups()
stop = time.time()
print(groups)
print(stop - start)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment