from collections import defaultdict from operator import itemgetter def main(): coordinates = read_coordinates() part_one(coordinates) part_two(coordinates) def read_coordinates(): coordinates = [] with open('input.txt', 'r') as input_file: index = 1 for line in input_file: cor_pair = [int(cor) for cor in line.strip().split(',')] cor_pair.append(index) coordinates.append(cor_pair) index += 1 return coordinates def part_one(coordinates): min_x = min([cor_pair[1] for cor_pair in coordinates]) max_x = max([cor_pair[1] for cor_pair in coordinates]) + 1 min_y = min([cor_pair[0] for cor_pair in coordinates]) max_y = max([cor_pair[0] for cor_pair in coordinates]) + 1 area_dict = defaultdict(int) for i in range(min_y, max_y): for j in range(min_x, max_x): h_distances = [[hamming_distance([i, j], cor_pair), cor_pair[2]] for cor_pair in coordinates] min_h_distance = min(h_distances, key=itemgetter(0)) count_min_h_distance = sum(1 for item in h_distances if item[0] == min_h_distance[0]) if count_min_h_distance == 1: area_dict[min_h_distance[1]] += 1 if i in (min_y, max_y - 1) or j in (min_x, max_x - 1): area_dict[min_h_distance[1]] = float('-inf') print max(area_dict.iteritems(), key=itemgetter(1)) def part_two(coordinates): min_x = min([cor_pair[1] for cor_pair in coordinates]) max_x = max([cor_pair[1] for cor_pair in coordinates]) + 1 min_y = min([cor_pair[0] for cor_pair in coordinates]) max_y = max([cor_pair[0] for cor_pair in coordinates]) + 1 total_distance_dict = defaultdict(int) for i in range(min_y, max_y): for j in range(min_x, max_x): total_distance_dict[(i, j)] = sum([hamming_distance([i, j], cor_pair) for cor_pair in coordinates]) region_size = 0 for _, value in total_distance_dict.iteritems(): if value < 10000: region_size += 1 print region_size def hamming_distance(a, b): return abs(a[0] - b[0]) + abs(a[1] - b[1]) if __name__ == '__main__': main()
Better late than never...
Had no time yesterday to write the solution down, but here it is.
Most of the time I struggled to understand how to filter "infinity areas". After I solved this it was a pretty easy.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Better late than never...
Had no time yesterday to write the solution down, but here it is.
Most of the time I struggled to understand how to filter "infinity areas". After I solved this it was a pretty easy.