Created
July 29, 2017 19:14
-
-
Save alexanderankin/12d9c23b47d4d3bdb126d73d1e899bc1 to your computer and use it in GitHub Desktop.
oh boy
This file contains hidden or 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
| #!/usr/bin/env python27 | |
| # 1:18 - 2:53 | |
| import pprint | |
| pp = pprint.PrettyPrinter(indent=4) | |
| # pp.pprint(stuff) | |
| def pprint(stuff): | |
| pp.pprint(stuff) | |
| test1 = """--#----#--- | |
| ###------#- | |
| ######----- | |
| -##-####--- | |
| ######---## | |
| ###-----### | |
| ##------### | |
| #-----#---- | |
| #---####--- | |
| -----##----""" | |
| # has five splin zones, only three of at least size 5 | |
| expected = (5, 3) | |
| def isZoneCell(current_location, relative_location, matrix): | |
| try: | |
| return matrix[current_location[0] + relative_location[0]][current_location[1] + relative_location[1]] == "#" | |
| except Exception as e: | |
| return False | |
| lastPointFound = None | |
| def findPointInZones(mypoint, zones): | |
| global lastPointFound | |
| for zone_index in range(len(zones)): | |
| zone = zones[zone_index] | |
| for point_index in range(len(zone)): | |
| point = zone[point_index] | |
| if point == mypoint: | |
| lastPointFound = { | |
| 'zone_index': zone_index | |
| } | |
| return True | |
| lastPointFound = None | |
| return False | |
| def countZones(input): | |
| matrix = input.split("\n") | |
| for i in range(len(matrix)): | |
| matrix[i] = list(matrix[i]) | |
| pprint(matrix) | |
| zones = [[(0, 7)]] | |
| for rowi in range(len(matrix)): | |
| for coli in range(len(matrix)): | |
| if matrix[rowi][coli] == "#": | |
| if not findPointInZones((rowi, coli), zones): | |
| hasplusneighborinzones = False | |
| """hasUPplusneighborinzones = isZoneCell((rowi, coli), (-1, 0), matrix) and findPointInZones((rowi - 1, coli), zones) | |
| UP_point = lastPointFound | |
| hasDNplusneighborinzones = isZoneCell((rowi, coli), (1, 0), matrix) and findPointInZones((rowi - 1, coli), zones) | |
| DN_point = lastPointFound | |
| hasLTplusneighborinzones = isZoneCell((rowi, coli), (0, -1), matrix) and findPointInZones((rowi - 1, coli), zones) | |
| LT_point = lastPointFound | |
| hasRTplusneighborinzones = isZoneCell((rowi, coli), (0, 1), matrix) and findPointInZones((rowi - 1, coli), zones) | |
| RT_point = lastPointFound | |
| # get all the zones of all non-None points: | |
| n_n_points = list(filter(lambda point: point is not None, [UP_point, DN_point, LT_point, RT_point])) | |
| pprint("okay") | |
| pprint(n_n_points) | |
| pprint("okay") | |
| n_n_points_zones = map(lambda point: point['zone_index'], n_n_points) | |
| pprint("nokay") | |
| pprint(n_n_points_zones) | |
| pprint(zones) | |
| pprint("nokay") | |
| n_n_points_zones = list(set(list(map(lambda point: point['zone_index'], n_n_points)))) | |
| pprint("nokay") | |
| pprint(n_n_points_zones) | |
| pprint("nokay") | |
| if len(n_n_points_zones) > 1: | |
| big_zone = [] | |
| for zone_index_ in n_n_points_zones: | |
| big_zone = list(set(big_zone + zones[zone_index_])) | |
| for zone_index_ in n_n_points_zones: | |
| print len(zones), zone_index_ | |
| del zones[zone_index_] | |
| zones.append(big_zone) | |
| elif len(n_n_points_zones) == 1: | |
| zones[n_n_points_zones[0]].append((rowi, coli))""" | |
| try: | |
| hasplusneighborinzones = hasplusneighborinzones or (isZoneCell((rowi, coli), (-1, 0), matrix) and findPointInZones((rowi - 1, coli), zones)) | |
| if hasplusneighborinzones: | |
| saved_point = lastPointFound | |
| # print("raising exception line 62") | |
| raise Exception | |
| hasplusneighborinzones = hasplusneighborinzones or (isZoneCell((rowi, coli), (1, 0), matrix) and findPointInZones((rowi + 1, coli), zones)) | |
| if hasplusneighborinzones: | |
| saved_point = lastPointFound | |
| # print("raising exception line 67") | |
| raise Exception | |
| hasplusneighborinzones = hasplusneighborinzones or (isZoneCell((rowi, coli), (0, -1), matrix) and findPointInZones((rowi, coli - 1), zones)) | |
| if hasplusneighborinzones: | |
| saved_point = lastPointFound | |
| # print("raising exception line 72") | |
| # print(saved_point) | |
| raise Exception | |
| hasplusneighborinzones = hasplusneighborinzones or (isZoneCell((rowi, coli), (0, 1), matrix) and findPointInZones((rowi, coli + 1), zones)) | |
| if hasplusneighborinzones: | |
| saved_point = lastPointFound | |
| # print("raising exception line 77") | |
| raise Exception | |
| except Exception as e: | |
| pass | |
| # raise e | |
| if hasplusneighborinzones: | |
| zones[saved_point['zone_index']].append((rowi, coli)) | |
| else: | |
| zones.append([(rowi, coli)]) | |
| """ | |
| if it is a plus, and it has neighbor pluses in zones, append to zone | |
| if plus neighbors not in zones, make own zone | |
| """ | |
| # try: | |
| # if isZoneCell((rowi, coli), (-1, 0)): # up | |
| return zones | |
| z = countZones(test1) | |
| pprint(z) | |
| def getEmptyMatrix(test_input): | |
| matrix = test_input.split("\n") | |
| for i in range(len(matrix)): | |
| matrix[i] = list(matrix[i]) | |
| for i in range(len(matrix)): | |
| for j in range(len(matrix)): | |
| matrix[i][j] = "-" | |
| return matrix | |
| def printZones(zones, test_input): | |
| """ | |
| test_input is only used to determine the size of the matrix""" | |
| for zone in zones: | |
| matrix = getEmptyMatrix(test_input) | |
| for point in zone: | |
| matrix[point[0]][point[1]] = '#' | |
| pprint(matrix) | |
| printZones(z, test1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment