2024 Dec 12th
This commit is contained in:
parent
f5a06a5a86
commit
69dbfddfae
1 changed files with 45 additions and 0 deletions
45
Python/2024/12/main.py
Normal file
45
Python/2024/12/main.py
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
input = open("input", 'r')
|
||||
|
||||
# d = [line for line in input]
|
||||
# d = [[val for val in line.strip()] for line in input]
|
||||
d = {(complex(x,y)): c for y,line in enumerate(input) for x,c in enumerate(line.strip())}
|
||||
|
||||
def run(p2=False):
|
||||
regionPrice = []
|
||||
s = set()
|
||||
for point, plot in d.items():
|
||||
if point in s: continue
|
||||
q = [point]
|
||||
area = 0
|
||||
sides = set()
|
||||
|
||||
while q:
|
||||
point = q.pop()
|
||||
if point in s: continue
|
||||
s.add(point)
|
||||
area += 1
|
||||
|
||||
for dr in {1,-1,1j,-1j}:
|
||||
npoint = point + dr
|
||||
if npoint in d and d[npoint] == plot:
|
||||
q.append(npoint)
|
||||
else:
|
||||
sides.add((dr, point))
|
||||
if p2:
|
||||
nsides = sides.copy()
|
||||
for side in sides:
|
||||
if side not in nsides:
|
||||
continue
|
||||
sdr, spoint = side
|
||||
for dr in {1,-1,1j,-1j} - {sdr, -sdr}:
|
||||
point = spoint + dr
|
||||
while (sdr, point) in nsides:
|
||||
nsides.discard((sdr,point))
|
||||
point = point + dr
|
||||
sides = nsides
|
||||
regionPrice.append(len(sides) * area)
|
||||
|
||||
return sum(regionPrice)
|
||||
|
||||
print(run())
|
||||
print(run(True))
|
||||
Loading…
Add table
Add a link
Reference in a new issue