Created
December 3, 2018 23:41
-
-
Save Dutcho/a159afddf03cc4f23829934bbb98c1f0 to your computer and use it in GitHub Desktop.
AoC03.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''AoC 2018 #03, safari-https://adventofcode.com/2018/day/3 - Olaf, 3 Dec 2018''' | |
import numpy, re | |
regexp = re.compile(r'#(\d+) @ (\d+),(\d+): (\d+)x(\d+)') | |
with open('AoC03.txt') as file: | |
data = numpy.array(tuple(tuple(map(int, regexp.match(line).groups())) for line in file)) | |
W, H = (max(data[..., 1 + dim] + data[..., 3 + dim]) for dim in range(2)) # max(x + w), max(y + h) | |
board = numpy.zeros((W + 1, H + 1), dtype=int) # size [0, W] x [0, H] | |
for id, x, y, w, h in data: | |
board[x : x + w, y : y + h] += 1 # cells used by id | |
print('first answer', sum(sum(board > 1))) # cells used by multiple id's | |
for id, x, y, w, h in data: | |
if all((board[x : x + w, y : y + h] == 1).flat): # all cells of id used by no other id's | |
print('second answer', id) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment