1
0
Fork 0

2018 day 3

Numpy is black magic.
This commit is contained in:
Lucidiot 2018-12-03 08:35:51 +01:00
parent 28d3677a68
commit 1a5d5866fd
No known key found for this signature in database
GPG Key ID: AE3F7205692FA205
2 changed files with 22 additions and 0 deletions

21
2018/3/fabric.py Executable file
View File

@ -0,0 +1,21 @@
#!/usr/bin/env python3
import fileinput
import re
import numpy as np
from collections import namedtuple
Claim = namedtuple('Claim', 'id, x, y, w, h')
def main():
fabric = np.zeros((1000, 1000))
regex = re.compile(r'^\s*#(\d+)\s+@\s+(\d+),(\d+)[:\s]+(\d+)x(\d+)\s*$')
claims = list(map(lambda x: Claim(*map(int, x)), map(next, map(iter, map(regex.findall, map(str.strip, fileinput.input()))))))
for claim in claims:
fabric[claim.x:claim.x+claim.w, claim.y:claim.y+claim.h] += 1
lonely = next(filter(lambda c: not np.sum(fabric[c.x:c.x+c.w, c.y:c.y+c.h] > 1), claims))
return np.sum(fabric > 1), lonely.id
if __name__ == '__main__':
print(main())

1
2018/3/requirements.txt Normal file
View File

@ -0,0 +1 @@
numpy>=1.15