a faster solution, ca 1:30 minut to 10 seconds
This commit is contained in:
parent
3ace709714
commit
9443543a77
1 changed files with 45 additions and 36 deletions
81
11/2.py
81
11/2.py
|
@ -1,12 +1,4 @@
|
||||||
def first_of(haystack, needles):
|
|
||||||
element = len(haystack) - 1
|
|
||||||
for needle in needles:
|
|
||||||
if needle in haystack:
|
|
||||||
element = min(haystack.index(needle), element)
|
|
||||||
return haystack[element] if haystack else ""
|
|
||||||
|
|
||||||
def count_adjacent(grid, line, seat):
|
def count_adjacent(grid, line, seat):
|
||||||
adjs = []
|
|
||||||
h = len(grid) - 1
|
h = len(grid) - 1
|
||||||
w = len(grid[0]) - 1
|
w = len(grid[0]) - 1
|
||||||
# generate lists
|
# generate lists
|
||||||
|
@ -16,65 +8,85 @@ def count_adjacent(grid, line, seat):
|
||||||
# D3 V2 D4
|
# D3 V2 D4
|
||||||
#
|
#
|
||||||
# vertical lists
|
# vertical lists
|
||||||
v1 = []
|
adj = 0
|
||||||
if line > 0:
|
if line > 0:
|
||||||
for x in range(line - 1, -1, -1):
|
for x in range(line - 1, -1, -1):
|
||||||
v1.append(grid[x][seat])
|
if grid[x][seat] == "#":
|
||||||
|
adj += 1
|
||||||
|
break
|
||||||
|
if grid[x][seat] == "L":
|
||||||
|
break
|
||||||
|
|
||||||
v2 = []
|
|
||||||
if line < w:
|
if line < w:
|
||||||
for x in range(line + 1, len(grid)):
|
for x in range(line + 1, len(grid)):
|
||||||
v2.append(grid[x][seat])
|
if grid[x][seat] == "#":
|
||||||
|
adj += 1
|
||||||
|
break
|
||||||
|
if grid[x][seat] == "L":
|
||||||
|
break
|
||||||
|
|
||||||
# horizontal lists
|
# horizontal lists
|
||||||
h1 = []
|
|
||||||
if seat > 0:
|
if seat > 0:
|
||||||
h1 = grid[line][seat - 1::-1]
|
for s in range(seat - 1, -1, -1):
|
||||||
h2 = grid[line][seat+1:]
|
if grid[line][s] == "#":
|
||||||
|
adj += 1
|
||||||
|
break
|
||||||
|
if grid[line][s] == "L":
|
||||||
|
break
|
||||||
|
for s in range(seat + 1, w+1):
|
||||||
|
if grid[line][s] == "#":
|
||||||
|
adj += 1
|
||||||
|
break
|
||||||
|
if grid[line][s] == "L":
|
||||||
|
break
|
||||||
|
|
||||||
|
|
||||||
# diagonal lists
|
# diagonal lists
|
||||||
d1 = []
|
|
||||||
s = seat - 1
|
s = seat - 1
|
||||||
l = line - 1
|
l = line - 1
|
||||||
for __ in range(min(line, seat)):
|
for __ in range(min(line, seat)):
|
||||||
d1.append(grid[l][s])
|
if grid[l][s] == "#":
|
||||||
|
adj += 1
|
||||||
|
break
|
||||||
|
if grid[l][s] == "L":
|
||||||
|
break
|
||||||
l -=1
|
l -=1
|
||||||
s -=1
|
s -=1
|
||||||
|
|
||||||
d2 = []
|
|
||||||
s = seat + 1
|
s = seat + 1
|
||||||
l = line - 1
|
l = line - 1
|
||||||
for __ in range(min(line, w - seat)):
|
for __ in range(min(line, w - seat)):
|
||||||
d2.append(grid[l][s])
|
if grid[l][s] == "#":
|
||||||
|
adj += 1
|
||||||
|
break
|
||||||
|
if grid[l][s] == "L":
|
||||||
|
break
|
||||||
l -=1
|
l -=1
|
||||||
s +=1
|
s +=1
|
||||||
|
|
||||||
d3 = []
|
|
||||||
s = seat - 1
|
s = seat - 1
|
||||||
l = line + 1
|
l = line + 1
|
||||||
for __ in range(min(h - line, seat)):
|
for __ in range(min(h - line, seat)):
|
||||||
d3.append(grid[l][s])
|
if grid[l][s] == "#":
|
||||||
|
adj += 1
|
||||||
|
break
|
||||||
|
if grid[l][s] == "L":
|
||||||
|
break
|
||||||
l +=1
|
l +=1
|
||||||
s -=1
|
s -=1
|
||||||
|
|
||||||
d4 = []
|
|
||||||
s = seat + 1
|
s = seat + 1
|
||||||
l = line + 1
|
l = line + 1
|
||||||
for __ in range(min(h - line, w - seat)):
|
for __ in range(min(h - line, w - seat)):
|
||||||
d4.append(grid[l][s])
|
if grid[l][s] == "#":
|
||||||
|
adj += 1
|
||||||
|
break
|
||||||
|
if grid[l][s] == "L":
|
||||||
|
break
|
||||||
l +=1
|
l +=1
|
||||||
s +=1
|
s +=1
|
||||||
|
|
||||||
adjs.append(first_of(v1, ["#", "L"]))
|
return adj
|
||||||
adjs.append(first_of(v2, ["#", "L"]))
|
|
||||||
adjs.append(first_of(h1, ["#", "L"]))
|
|
||||||
adjs.append(first_of(h2, ["#", "L"]))
|
|
||||||
adjs.append(first_of(d1, ["#", "L"]))
|
|
||||||
adjs.append(first_of(d2, ["#", "L"]))
|
|
||||||
adjs.append(first_of(d3, ["#", "L"]))
|
|
||||||
adjs.append(first_of(d4, ["#", "L"]))
|
|
||||||
|
|
||||||
return adjs.count("#")
|
|
||||||
|
|
||||||
with open("input") as f:
|
with open("input") as f:
|
||||||
content = [[y for y in x.strip()] for x in f]
|
content = [[y for y in x.strip()] for x in f]
|
||||||
|
@ -88,7 +100,6 @@ changes = 1
|
||||||
iterations = 0
|
iterations = 0
|
||||||
while changes > 0:
|
while changes > 0:
|
||||||
iterations += 1
|
iterations += 1
|
||||||
# print(".", end="", flush=True)
|
|
||||||
last = []
|
last = []
|
||||||
for line in new:
|
for line in new:
|
||||||
last.append(line.copy())
|
last.append(line.copy())
|
||||||
|
@ -106,8 +117,6 @@ while changes > 0:
|
||||||
new[line][seat] = "L"
|
new[line][seat] = "L"
|
||||||
changes += 1
|
changes += 1
|
||||||
# print("".join(adjs), "".join(new[line]))
|
# print("".join(adjs), "".join(new[line]))
|
||||||
# for line in new:
|
|
||||||
# print("".join(line))
|
|
||||||
|
|
||||||
#for line in new:
|
#for line in new:
|
||||||
# print("".join(line))
|
# print("".join(line))
|
||||||
|
|
Loading…
Add table
Reference in a new issue