aboutsummaryrefslogtreecommitdiff
path: root/day3/solve.py
diff options
context:
space:
mode:
authorromangraef <romangraef@loves.dicksinhisan.us>2018-12-03 19:11:12 +0100
committerromangraef <romangraef@loves.dicksinhisan.us>2018-12-03 19:11:12 +0100
commit8b654aa752efa50c9407dbe4fd8ed567dafbeb45 (patch)
tree8846c617a1f1852e8d7e3d2d01af10bc874dceb8 /day3/solve.py
parent92927216a4cdc37d955e197d26c94ae2e8041366 (diff)
downloadaoc2018-8b654aa752efa50c9407dbe4fd8ed567dafbeb45.tar.gz
aoc2018-8b654aa752efa50c9407dbe4fd8ed567dafbeb45.tar.bz2
aoc2018-8b654aa752efa50c9407dbe4fd8ed567dafbeb45.zip
day3 solution
Diffstat (limited to 'day3/solve.py')
-rw-r--r--day3/solve.py58
1 files changed, 58 insertions, 0 deletions
diff --git a/day3/solve.py b/day3/solve.py
index e69de29..92be090 100644
--- a/day3/solve.py
+++ b/day3/solve.py
@@ -0,0 +1,58 @@
+import re
+
+import numpy as np
+
+from commons import get_input
+
+
+def parse_line(line):
+ match = pattern.match(line)
+ if not match:
+ return
+ return [int(g) for g in match.groups()]
+
+
+pattern = re.compile(r'^#(\d+) @ (\d+),(\d+): (\d+)x(\d+)$')
+grid = np.zeros((1000, 1000), dtype=int)
+
+
+def insert_values():
+ global grid
+ for _, x, y, w, h in selection:
+ for i in range(w):
+ for j in range(h):
+ grid[x + i, y + j] += 1
+
+
+def count_overlaps():
+ c = 0
+ for i in range(1000):
+ for j in range(1000):
+ if grid[i, j] > 1:
+ c += 1
+ return c
+
+
+def is_overlapping(x, y, w, h):
+ for i in range(w):
+ for j in range(h):
+ if grid[x + i, y + j] != 1:
+ return True
+ return False
+
+
+def find_unique():
+ for id, x, y, w, h in selection:
+ if not is_overlapping(x, y, w, h):
+ return id
+
+
+lines = get_input(3).splitlines(keepends=False)
+
+selection = [parse_line(line) for line in lines]
+
+insert_values()
+
+if __name__ == '__main__':
+ print('first:', count_overlaps())
+ print('second:', find_unique())