diff --git a/6/a.py b/6/a.py new file mode 100755 index 0000000..ae2a418 --- /dev/null +++ b/6/a.py @@ -0,0 +1,89 @@ +#!/usr/bin/env python3 + + +def getdist(pa, pb): + return abs(pa[0] - pb[0]) + abs(pa[1] - pb[1]) + + +def fullrange(s, e): + return range(s, e + 1) + + +def get_closest(point, allpoints): + """ + Returns the index of the point from allpoints closest to the passed point. + Returns None if two points are equidistant. + """ + best_index = None + best_distance = 999999999 + is_dupe = False + + for index, p in enumerate(allpoints): + # if p == point: + # continue + dist = getdist(point, p) + if dist <= best_distance: + if dist == best_distance: + is_dupe = True + else: + is_dupe = False + best_distance = dist + best_index = index + + if is_dupe: + return None + + return best_index + + +def main(): + coords = [] + + with open("input.txt") as f: + for line in f.readlines(): + a, b = line.split(", ") + coords.append((int(a), int(b))) + + # bounding box is be +x+y, -x,-y corners + xcoords = [i[0] for i in coords] + ycoords = [i[1] for i in coords] + bufsize = 1 + bbox = ( + ( + max(xcoords) + bufsize, + max(ycoords) + bufsize + ), + ( + min(xcoords) - bufsize, + min(ycoords) - bufsize + ) + ) + + # point ids (their index in the coords list) mapped to their size count + fields = {x: 0 for x in range(0, len(coords))} + infinte_fields = set() + for y in fullrange(bbox[1][1], bbox[0][1]): + for x in fullrange(bbox[1][0], bbox[0][0]): + closest = get_closest((x, y), coords) + if closest is not None: + fields[closest] += 1 + + # if touching an edge of the bounding box + if x == bbox[1][0] or x == bbox[0][0] or y == bbox[1][1] or y == bbox[0][1]: + infinte_fields.update([closest]) + + biggest_fieldid = None + biggest_field_size = 0 + + for pointid, size in fields.items(): + if pointid not in infinte_fields: + if size > biggest_field_size: + biggest_field_size = size + biggest_fieldid = pointid + + print("biggest field id:", biggest_fieldid) + print("biggest field size:", biggest_field_size) + + +if __name__ == '__main__': + main() diff --git a/6/b.py b/6/b.py new file mode 100755 index 0000000..a19ce3c --- /dev/null +++ b/6/b.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 + + +from a import getdist, fullrange + + +def point_vaild(point, allpoints, units): + """ + Returns True if the point is no more than units away from all points in allpoints (as in the sum of all dists) + """ + + for p in allpoints: + units -= getdist(point, p) + if units <= 0: + return False + return True + + +def main(): + coords = [] + maxdist = 10000 + + with open("input.txt") as f: + for line in f.readlines(): + a, b = line.split(", ") + coords.append((int(a), int(b))) + + # bounding box is be +x+y, -x,-y corners + # TBD this probably breaks with abusive input + xcoords = [i[0] for i in coords] + ycoords = [i[1] for i in coords] + bufsize = 0 + bbox = ( + ( + max(xcoords) + bufsize, + max(ycoords) + bufsize + ), + ( + min(xcoords) - bufsize, + min(ycoords) - bufsize + ) + ) + + print("bbox:", bbox) + + valids = 0 + for y in fullrange(bbox[1][1], bbox[0][1]): + for x in fullrange(bbox[1][0], bbox[0][0]): + if point_vaild((x, y), coords, maxdist): + valids += 1 + + print(valids) + + +if __name__ == '__main__': + main() diff --git a/6/input.txt b/6/input.txt new file mode 100644 index 0000000..767ee7d --- /dev/null +++ b/6/input.txt @@ -0,0 +1,50 @@ +305, 349 +315, 193 +154, 62 +246, 310 +145, 283 +260, 324 +342, 79 +321, 353 +40, 242 +351, 353 +337, 297 +174, 194 +251, 160 +314, 195 +114, 81 +204, 246 +203, 169 +203, 296 +60, 276 +201, 47 +206, 96 +243, 46 +295, 304 +319, 80 +213, 330 +337, 255 +40, 262 +302, 150 +147, 349 +317, 240 +96, 315 +133, 305 +320, 348 +210, 300 +266, 216 +223, 319 +207, 152 +127, 214 +312, 245 +49, 329 +211, 84 +129, 276 +247, 143 +208, 235 +271, 126 +124, 211 +144, 184 +54, 88 +354, 300 +148, 85