From 17692e5d24a357adfc2f278e4a09bdbe1e07d0f4 Mon Sep 17 00:00:00 2001 From: Jack Bond-Preston Date: Thu, 6 Dec 2018 15:41:28 +0000 Subject: [PATCH] Day 6 --- aoc-6/inputs.txt | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ aoc-6/main.py | 33 ++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 aoc-6/inputs.txt create mode 100644 aoc-6/main.py diff --git a/aoc-6/inputs.txt b/aoc-6/inputs.txt new file mode 100644 index 0000000..0100808 --- /dev/null +++ b/aoc-6/inputs.txt @@ -0,0 +1,50 @@ +336, 308 +262, 98 +352, 115 +225, 205 +292, 185 +166, 271 +251, 67 +266, 274 +326, 85 +191, 256 +62, 171 +333, 123 +160, 131 +211, 214 +287, 333 +231, 288 +237, 183 +211, 272 +116, 153 +336, 70 +291, 117 +156, 105 +261, 119 +216, 171 +59, 343 +50, 180 +251, 268 +169, 258 +75, 136 +305, 102 +154, 327 +187, 297 +270, 225 +190, 185 +339, 264 +103, 301 +90, 92 +164, 144 +108, 140 +189, 211 +125, 157 +77, 226 +177, 168 +46, 188 +216, 244 +346, 348 +272, 90 +140, 176 +109, 324 +128, 132 \ No newline at end of file diff --git a/aoc-6/main.py b/aoc-6/main.py new file mode 100644 index 0000000..2aeb394 --- /dev/null +++ b/aoc-6/main.py @@ -0,0 +1,33 @@ +from collections import defaultdict + +input = open("inputs.txt", "r").read().split('\n') + +coords = [ (int(x.split(',')[0]), int(x.split(',')[1])) for x in input ] + +minCoords = (min(coords, key = lambda x: x[0])[0], min(coords, key = lambda x: x[1])[1]) +maxCoords = (max(coords, key = lambda x: x[0])[0], max(coords, key = lambda x: x[1])[1]) + +areas = defaultdict(int) +largerAreas = defaultdict(int) +safeArea = 0 + +def manhattan_distance(coord1, coord2): + return abs(coord1[0] - coord2[0]) + abs(coord1[1] - coord2[1]) + +for x in range(minCoords[0] - 1, maxCoords[0] + 2): + for y in range(minCoords[1] - 1, maxCoords[1] + 2): + closestCoord = min(coords, key = lambda c: manhattan_distance(c, (x, y))) + + distances = sum([ manhattan_distance(c, (x, y)) for c in coords ]) + if (distances < 10000): + safeArea += 1 + + largerAreas[closestCoord] += 1 + + if (x >= minCoords[0] and x <= maxCoords[0] and y >= minCoords[1] and y <= maxCoords[1]): + areas[closestCoord] += 1 + +unchangedAreas = { k: v for k, v in areas.items() if v == largerAreas[k] } + +print(f"[Part 1] Largest non-infinite area: { max(unchangedAreas.values()) }") +print(f"[Part 2] Safe area: { safeArea }") \ No newline at end of file