Do day 18.

This commit is contained in:
2023-12-19 15:52:36 -05:00
parent 5626205b2f
commit 30a8489313
3 changed files with 145 additions and 2 deletions

11
lib.py
View File

@@ -31,6 +31,8 @@ class Grid2D:
NE = (-1, 1)
SE = (1, 1)
SW = (1, -1)
COORDS_ORTH = (N, E, S, W)
COORDS_DIAG = (NW, NE, SE, SW)
def __init__(self, text: str):
lines = [line for line in text.splitlines() if line.strip() != ""]
@@ -222,3 +224,12 @@ class A_Star(object):
g_score[neighbor] = tentative_g_score
f_score = g_score[neighbor] + h(neighbor)
heapq.heappush(open_set, (f_score, neighbor))
def shoelace_area(corners):
n = len(corners)
area = 0
for i in range(n):
x1, y1 = corners[i]
x2, y2 = corners[(i + 1) % n]
area += (x1 * y2) - (x2 * y1)
return abs(area) / 2.0