Skip to content

Instantly share code, notes, and snippets.

@msgodf
Last active December 9, 2015 23:29
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 msgodf/4344921 to your computer and use it in GitHub Desktop.
Save msgodf/4344921 to your computer and use it in GitHub Desktop.
Python solution to the "Radiation Search" problem on check.io
from random import randint
# A grid of the specified size, composed of random values in range [1,n]
def random_grid(size, n):
return [[randint(0, (n - 1)) for _ in range(size)] for _ in range(size)]
# Generate all sets of integers pairs in [0,n), excluding those of the form [a a]
def int_pairs(n):
pairs = []
for i in range(0, n):
pairs = pairs + zip([i] * (n - i), range(i + 1 , n))
return pairs
# Replaces the first cluster by it merged with the second cluster, then removes the second cluster
def merge_clusters(clusters, first_index, second_index):
clusters[first_index] = clusters[first_index] + clusters[second_index]
clusters.pop(second_index)
return clusters
# Check whether at least one point in the first cluster is adjacent to another in the other cluster
def clusters_adjacent(first_cluster, second_cluster):
for point_a in first_cluster:
for point_b in second_cluster:
if adjacent(point_a, point_b):
return True
return False
# Check all pairs of clusters, and returns the first pair that are adjacent, or (-1,-1) if none are adjacent
def first_adjacent_clusters(clusters):
for pair in int_pairs(len(clusters)):
first, second = pair
if clusters_adjacent(clusters[first], clusters[second]):
return pair
return (-1, -1)
# Given two pairs of (x,y) coordinates, checks whether they are adjacent to each other
def adjacent(point_a, point_b):
x1, y1 = point_a
x2, y2 = point_b
return ((x1 == x2) and (((y1 - 1) == y2) or ((y1 + 1) == y2))) or ((y1 == y2) and (((x1 - 1) == x2) or ((x1 + 1) == x2)))
# The coordinates of the n's within the grid
def grid_to_clusters(grid, n):
output = []
for y in range(0, len(grid)):
for x in range(0, len(grid)):
if grid[y][x] == n:
output.append([[y, x]])
return output
# The core of the program, checks for adjacent clusters and merges them
def check_and_merge(clusters):
pair = first_adjacent_clusters(clusters)
while pair != (-1,-1):
first, second = pair
clusters = merge_clusters(clusters, first, second)
pair = first_adjacent_clusters(clusters)
return clusters
def max_value_in_grid(grid):
return max(map(max, grid))
def biggest_cluster_of_n(grid, n):
clusters_of_n = check_and_merge(grid_to_clusters(grid, n))
if len(clusters_of_n) >0:
return max(map(len, clusters_of_n))
else:
return 0
# Find the number that has the biggest cluster within the grid
def biggest_cluster(grid):
biggest_clusters = list(map(lambda n: biggest_cluster_of_n(grid, n), range(max_value_in_grid(grid) + 1)))
return max(zip(biggest_clusters, range(len(biggest_clusters))))
@msgodf
Copy link
Author

msgodf commented Dec 20, 2012

I'm sure this could be a lot better if my Python wasn't such a mishmash of functional and imperative

@msgodf
Copy link
Author

msgodf commented Dec 22, 2012

Turns out that there are much simpler solutions on check.io

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment