-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.py
62 lines (42 loc) · 1.06 KB
/
solver.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
60
61
62
import os
import inspect
cwd = os.path.dirname(os.path.abspath(inspect.stack()[0][1]))
with open(os.path.join(cwd, "example.txt"), "r") as f:
ops = [
(d, int(a), c[2:-1])
for d, a, c in [line.split() for line in f.read().splitlines()]
]
def get_pos(direction):
match direction:
case "R":
return 1
case "L":
return -1
case "U":
return -1j
case "D":
return 1j
def get_lava_amount(p1, d, a):
p2 = p1 + get_pos(d) * a
area = 0
# shoelace formula
area += p1.real * p2.imag
area -= p2.real * p1.imag
# make sure we include the outline
area += a
return area
# part 1
curr_pos = 0
res = 0
for d, a, _ in ops:
res += get_lava_amount(curr_pos, d, a)
curr_pos += get_pos(d) * a
print(int(res / 2) + 1)
# part 2
curr_pos = 0
res = 0
for _, _, h in ops:
d, a = h[-1].translate(str.maketrans("0123", "RDLU")), int(h[:-1], 16)
res += get_lava_amount(curr_pos, d, a)
curr_pos += get_pos(d) * a
print(int(res / 2) + 1)