-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay18_LavaductLagoon.py
58 lines (48 loc) · 1.65 KB
/
Day18_LavaductLagoon.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
import numpy as np
def parse_input(text):
text = [line.replace('\n', '') for line in text] # strip \n
dirs, lens, cols = [], [], []
for line in text:
direction, length, color = line.split(' ')
dirs.append(direction)
lens.append(int(length))
cols.append(color)
return dirs, lens, cols
def area(points):
# Shoelace formula
total_area = 0
for i in range(len(points)):
x1, y1 = points[i]
x2, y2 = points[(i + 1) % len(points)]
total_area += (int(x1) * int(y2) - int(x2) * int(y1))
return abs(total_area) / 2
def solve1(dirs, lens, cols):
cur_coord = np.array((0,0))
digsite = [cur_coord]
directions = {'R': np.array((0, 1)), 'L': np.array((0, -1)), 'U': np.array((-1, 0)), 'D': np.array((1, 0))}
total_len = 0
for d, l in zip(dirs, lens):
total_len += l
cur_coord += directions[d] * l
digsite.append(np.copy(cur_coord))
digsite = np.array(digsite)
total_area = area(digsite)
additional_area = (total_len / 2) + 1
return int(total_area + additional_area)
def solve2(dirs, lens, cols):
# Oh no, someone has switched colors and directions columns!
new_dirs = []
new_lens = []
dirs = ['R', 'D', 'L', 'U']
for color in cols:
hex = color[2:-2]
dir = color[-2]
new_dirs.append(dirs[int(dir)])
new_lens.append(int(hex, base=16))
return solve1(new_dirs, new_lens, cols)
if __name__ == "__main__":
path = "Day18.txt"
with open(path) as f:
text = f.readlines()
data = parse_input(text)
print(solve2(*data))