day 15 p1 and p2 partial
This commit is contained in:
67
15/o.py
Normal file
67
15/o.py
Normal file
@@ -0,0 +1,67 @@
|
||||
import os
|
||||
|
||||
input = open(os.path.dirname(__file__) +
|
||||
"/input-demo.txt", "r").readlines()
|
||||
|
||||
beacons = []
|
||||
sensors = []
|
||||
distance = []
|
||||
|
||||
for line in input:
|
||||
l = line.strip().split()
|
||||
sx = int(l[2].split('=')[1].split(',')[0])
|
||||
sy = int(l[3].split('=')[1].split(':')[0])
|
||||
bx = int(l[8].split('=')[1].split(',')[0])
|
||||
by = int(l[9].split('=')[1])
|
||||
d = abs(bx-sx)+abs(by-sy)
|
||||
distance.append(d)
|
||||
beacons.append((bx, by))
|
||||
sensors.append((sx, sy))
|
||||
print(sx, sy, bx, by, d)
|
||||
|
||||
|
||||
def getcoverage(b, Y):
|
||||
coverage = set()
|
||||
(x, y) = sensors[b]
|
||||
d0 = distance[i]
|
||||
print("check", x, y, d0)
|
||||
for X in range(x-d0, x+d0):
|
||||
if (abs(x-X)+abs(y-Y)) <= d0:
|
||||
coverage.add((X, Y))
|
||||
|
||||
# for Y in range(y-d0, y+d0):
|
||||
#print(X, Y, abs(x-X)+abs(y-Y))
|
||||
|
||||
return coverage
|
||||
|
||||
|
||||
#testy = 2000000
|
||||
testy = 10
|
||||
|
||||
fullcoverage = set()
|
||||
for i in range(len(sensors)):
|
||||
cov = getcoverage(i, testy)
|
||||
# print(cov)
|
||||
fullcoverage.update(cov)
|
||||
|
||||
# print(fullcoverage)
|
||||
|
||||
|
||||
testset = set()
|
||||
for s in fullcoverage:
|
||||
if s[1] == testy:
|
||||
testset.add(s)
|
||||
|
||||
nonbeacons = testset.difference(set(beacons))
|
||||
|
||||
# print(sorted(set(beacons)))
|
||||
# print(sorted(testset))
|
||||
|
||||
|
||||
for b in beacons:
|
||||
if b[1] == testy:
|
||||
print(b[0]*4000000+b[1])
|
||||
|
||||
|
||||
print(len(testset))
|
||||
print(len(nonbeacons))
|
||||
Reference in New Issue
Block a user