Skip to content

Instantly share code, notes, and snippets.

@Dutcho
Created December 3, 2018 23:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dutcho/a159afddf03cc4f23829934bbb98c1f0 to your computer and use it in GitHub Desktop.
Save Dutcho/a159afddf03cc4f23829934bbb98c1f0 to your computer and use it in GitHub Desktop.
AoC03.py
'''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