day 12 part 2
parent
1b49b24aa1
commit
54c84304aa
26
day12/sol.py
26
day12/sol.py
|
@ -28,6 +28,7 @@ def solve(file):
|
|||
|
||||
done = set() # set of points which have been counted as part of a shape
|
||||
total = 0
|
||||
total2 = 0
|
||||
for p in points():
|
||||
if p in done: continue
|
||||
shape = flood(p)
|
||||
|
@ -38,8 +39,33 @@ def solve(file):
|
|||
perim += 4-n
|
||||
#print(get(p), area, perim)
|
||||
total += area * perim
|
||||
total2 += area * discount(shape)
|
||||
|
||||
print(total)
|
||||
print(total2)
|
||||
|
||||
def discount(shape):
|
||||
def sides(p):
|
||||
x,y = p
|
||||
s = 0
|
||||
if (x+1,y) in shape: s += 1
|
||||
if (x-1,y) in shape: s += 2
|
||||
if (x,y+1) in shape: s += 4
|
||||
if (x,y-1) in shape: s += 8
|
||||
return s
|
||||
|
||||
# only count a side as free if the neighbor to the north or west
|
||||
# does not have the same side free
|
||||
perim = 0
|
||||
for p in shape:
|
||||
x,y = p
|
||||
free = sides(p) ^ 15
|
||||
if (x-1,y) in shape:
|
||||
free &= sides((x-1,y))
|
||||
if (x,y-1) in shape:
|
||||
free &= sides((x,y-1))
|
||||
perim += free.bit_count()
|
||||
return perim
|
||||
|
||||
solve(open("sample4.in"))
|
||||
solve(open("input"))
|
||||
|
|
Loading…
Reference in New Issue