Browse Source

a faster solution, ca 1:30 minut to 10 seconds

Juan José Gutiérrez de Quevedo Pérez 4 months ago
parent
commit
9443543a77
1 changed files with 45 additions and 36 deletions
  1. 45 36
      11/2.py

+ 45 - 36
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):
-    adjs = []
     h = len(grid) - 1
     w = len(grid[0]) - 1
     # generate lists
@@ -16,65 +8,85 @@ def count_adjacent(grid, line, seat):
     #   D3  V2  D4
     #
     # vertical lists
-    v1 = []
+    adj = 0
     if line > 0:
         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:
         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
-    h1 = []
     if seat > 0:
-        h1 = grid[line][seat - 1::-1]
-    h2 = grid[line][seat+1:]
+        for s in range(seat - 1, -1, -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
-    d1 = []
     s = seat - 1
     l = line - 1
     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
         s -=1
 
-    d2 = []
     s = seat + 1
     l = line - 1
     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
         s +=1
 
-    d3 = []
     s = seat - 1
     l = line + 1
     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
         s -=1
 
-    d4 = []
     s = seat + 1
     l = line + 1
     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
         s +=1
 
-    adjs.append(first_of(v1, ["#", "L"]))
-    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("#")
+    return adj
 
 with open("input") as f:
     content = [[y for y in x.strip()] for x in f]
@@ -88,7 +100,6 @@ changes = 1
 iterations = 0
 while changes > 0:
     iterations += 1
-#    print(".", end="", flush=True)
     last = []
     for line in new:
         last.append(line.copy())
@@ -106,8 +117,6 @@ while changes > 0:
                 new[line][seat] = "L"
                 changes += 1
 #        print("".join(adjs), "".join(new[line]))
-#    for line in new:
-#        print("".join(line))
 
 #for line in new:
 #    print("".join(line))