Do day 13 and add cleaner solution to day 18.

This commit is contained in:
2023-12-24 11:51:31 -05:00
parent 30a8489313
commit 053d07dd27
4 changed files with 115 additions and 7 deletions

22
d18.py
View File

@@ -51,8 +51,7 @@ def solve(i: Input, second=False):
to_visit.append(nc)
return len(set(coords))
def calc(ins):
def area(ins):
c = (0, 0)
corners = []
for i, (count, d) in enumerate(ins):
@@ -86,6 +85,20 @@ def calc(ins):
return int(shoelace_area(corners))
def area2(ins):
# Solution based on Shoelace area and the idea that the outside area is
# perimate * 1 // 2, and then +1 (for four quarter corners). All other
# corners cancel out to zero.
c = (0, 0)
corners = []
perimeter = 0
for i, (count, d) in enumerate(ins):
c = (c[0] + count * d[0], c[1] + count * d[1])
corners.append(c)
perimeter += count
return int(shoelace_area(corners)) + perimeter // 2 + 1
def solve2(i: Input, second=False):
lines = i.lines()
ins = []
@@ -97,7 +110,8 @@ def solve2(i: Input, second=False):
d = int(c[:5], 16)
m = {"0": Grid2D.E, "1": Grid2D.S, "2": Grid2D.W, "3": Grid2D.N}[c[5]]
ins.append((d, m))
return calc(ins)
assert area(ins) == area2(ins)
return area(ins)
def debug():
@@ -113,7 +127,7 @@ def debug():
]
# Should be 14 but is 2 because it's going counter-clockwise, but mapping
# only works for clockwise.
print(calc(ins))
print(area(ins))
def main():