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:
31
day_18/part1.py
Normal file
31
day_18/part1.py
Normal file
@@ -0,0 +1,31 @@
|
||||
import numpy as np
|
||||
|
||||
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_line(line):
|
||||
(direction, amount, _) = line.split()
|
||||
return direction, int(amount)
|
||||
|
||||
direction_map = {"L": (0,-1), "R": (0,1), "D": (-1,0), "U": (1,0)}
|
||||
|
||||
def process(input):
|
||||
points = []
|
||||
perimeter_area = 0
|
||||
current_point = (0,0)
|
||||
for line in input.split("\n"):
|
||||
(dir_letter, amount) = process_line(line)
|
||||
direction = (direction_map[dir_letter][0] * amount, direction_map[dir_letter][1] * amount)
|
||||
perimeter_area += amount
|
||||
current_point = (current_point[0] + direction[0], current_point[1] + direction[1])
|
||||
points.append(current_point)
|
||||
indices = list(range(len(points) - 1)) + [-1]
|
||||
determinants = [np.linalg.det(np.array([[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