-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday18.py
61 lines (40 loc) · 1.63 KB
/
day18.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# /******** Part1 *******
def parse_input():
with open("input.txt") as file:
return [(line.split()[0], int(line.split()[1])) for line in file]
def dig(plan):
start_x, start_y = 0, 0
trenches = []
directions = {'U': (-1, 0), 'D': (1, 0), 'L': (0, -1), 'R': (0, 1)}
for direction, steps in plan:
for i in range(steps + 1):
new_x = start_x + directions[direction][0] * i
new_y = start_y + directions[direction][1] * i
trenches.append((new_x, new_y))
start_x, start_y = new_x, new_y
return trenches
def polygon_area(vertices): # shoelace formula
n = len(vertices)
area = 0.5 * abs(
sum(vertices[i][0] * vertices[(i + 1) % n][1] - vertices[(i + 1) % n][0] * vertices[i][1] for i in range(n)))
return area
def interior_points(area, boundary_count): # pick's theorem
return area - boundary_count / 2 + 1
def count_trenches(boundaries, plan): # combines above formulas to count all points in the polygon
boundary_count = sum([line[1] for line in plan])
area = polygon_area(boundaries)
return int(interior_points(area, boundary_count) + boundary_count)
def calculate_part_1():
plan = parse_input()
trenches = dig(plan)
return count_trenches(trenches, plan)
# /******** Part2 *******
def parse_part_2():
directions = {0: 'R', 1: 'D', 2: 'L', 3: 'U'}
with open("input.txt") as file:
plan = [(directions[int(line.split()[2][-2])], int(line.split()[2][2:-2], 16)) for line in file]
return plan
def calculate_part_2():
plan = parse_part_2()
trenches = dig(plan)
return count_trenches(trenches, plan)