|
|
@@ -1,32 +1,19 @@
|
|
|
#!/usr/bin/env python3
|
|
|
import sys
|
|
|
-from itertools import pairwise
|
|
|
+from itertools import accumulate, pairwise
|
|
|
|
|
|
-def parse(inp):
|
|
|
- DXDY = (1, 0), (0, 1), (-1, 0), (0, -1)
|
|
|
- for line in inp:
|
|
|
- direction, distance, color = line.split()
|
|
|
- normal_instruction = DXDY['RDLU'.index(direction)], int(distance)
|
|
|
- color_instruction = DXDY[int(color[7])], int(color[2:7], 16)
|
|
|
- yield normal_instruction, color_instruction
|
|
|
+def parse(line):
|
|
|
+ direction, distance, color = line.split()
|
|
|
+ d = int(distance)
|
|
|
+ yield ((d, 0), (0, d), (-d, 0), (0, -d))['RDLU'.index(direction)]
|
|
|
+ d = int(color[2:7], 16)
|
|
|
+ yield ((d, 0), (0, d), (-d, 0), (0, -d))[int(color[7])]
|
|
|
|
|
|
-def follow(instructions):
|
|
|
- x = y = 0
|
|
|
- for (dx, dy), distance in instructions:
|
|
|
- x += dx * distance
|
|
|
- y += dy * distance
|
|
|
- yield x, y
|
|
|
+def dig(steps):
|
|
|
+ xy = list(zip(*map(accumulate, zip(*steps))))
|
|
|
+ return sum(xa * yb - ya * xb + abs(xb - xa + yb - ya)
|
|
|
+ for (xa, ya), (xb, yb) in pairwise(xy + xy[:1])) // 2 + 1
|
|
|
|
|
|
-def dig(instructions):
|
|
|
- x, y = zip(*follow(instructions))
|
|
|
- v = len(x)
|
|
|
- area = abs(sum(x[i] * y[(i + 1) % v] - y[i] * x[(i + 1) % v]
|
|
|
- for i in range(v))) // 2 # Shoelace formula
|
|
|
- outer = sum(abs(bx - ax + by - ay)
|
|
|
- for (ax, ay), (bx, by) in pairwise(zip(x + x[:1], y + y[:1])))
|
|
|
- inner = area - outer // 2 + 1 # Pick's theorem
|
|
|
- return outer + inner
|
|
|
-
|
|
|
-normal, color = zip(*parse(sys.stdin))
|
|
|
-print(dig(normal))
|
|
|
+wrong, color = zip(*map(parse, sys.stdin))
|
|
|
+print(dig(wrong))
|
|
|
print(dig(color))
|