This day was pretty easy, though I made a stupid mistake and took too long. Also started way late. But only 13 lines total for part 2
This commit is contained in:
27
day_18/part2.py
Normal file
27
day_18/part2.py
Normal file
@@ -0,0 +1,27 @@
|
||||
from numpy.linalg import det
|
||||
|
||||
def test(input, solution):
|
||||
# print(f"Running input {input}")
|
||||
result = process(input)
|
||||
print(f"Solution was {result}")
|
||||
assert result == solution
|
||||
|
||||
def run():
|
||||
file = open("input.txt", "r")
|
||||
return process(file.read()[:-1])
|
||||
|
||||
|
||||
def process(input):
|
||||
points = []
|
||||
perimeter_area = 0
|
||||
current_point = (0,0)
|
||||
for line in input.split("\n"):
|
||||
dir_letter = {"2": "L", "0": "R", "1": "D", "3": "U"}[line.split()[2][7]]
|
||||
amount = int(line.split()[2][2:7], 16)
|
||||
direction = [amount * x for x in {"L": (0,-1), "R": (0,1), "D": (-1,0), "U": (1,0)}[dir_letter]]
|
||||
perimeter_area += amount
|
||||
current_point = [sum(x) for x in zip(current_point, direction)]
|
||||
points.append(current_point)
|
||||
indices = list(range(len(points) - 1)) + [-1]
|
||||
determinants = [det([[points[i][1], points[i+1][1]],[points[i][0], points[i+1][0]]]) for i in indices]
|
||||
return round(abs(sum(determinants))/2) + perimeter_area/2 + 1
|
||||
Reference in New Issue
Block a user