You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 satır
3.5KB

  1. def perimeter(pts):
  2. n = 0
  3. for i in range(len(pts)):
  4. dist = pts[i - 1] - pts[i]
  5. n += abs(dist.real) + abs(dist.imag)
  6. return int(n)
  7. def area(pts):
  8. # shoelace method
  9. a = 0
  10. for i in range(len(pts)):
  11. p1, p2 = pts[i - 1], pts[i]
  12. a += p1.real * p2.imag - p2.real * p1.imag
  13. return int(a / 2)
  14. text = open(0).read()
  15. pts = [0]
  16. for line in text.splitlines():
  17. d, n, _ = line.split()
  18. n = int(n)
  19. d = 1j ** 'RDLU'.index(d)
  20. pts.append(pts[-1] + d * n)
  21. print(area(pts) + perimeter(pts) // 2 + 1)
  22. pts = [0]
  23. for line in text.splitlines():
  24. _, _, s = line.split()
  25. n = int(s[2:7], 16)
  26. d = 1j ** int(s[7])
  27. pts.append(pts[-1] + d * n)
  28. print(area(pts) + perimeter(pts) // 2 + 1)
  29. # def render(graph, default='.'):
  30. # xmin, *_, xmax = sorted(int(p.real) for p in graph)
  31. # ymin, *_, ymax = sorted(int(p.imag) for p in graph)
  32. # out = ''
  33. # for y in range(ymin, ymax + 1):
  34. # for x in range(xmin, xmax + 1):
  35. # out += graph.get(complex(x, y), default)
  36. # out += '\n'
  37. # return out
  38. # def connected_components(graph):
  39. # groups = []
  40. # while graph:
  41. # seen = set()
  42. # edge = {graph.pop()}
  43. # while edge:
  44. # seen |= edge
  45. # edge = {pp + ss for pp in edge for ss in [1, -1, 1j, -1j] if pp + ss in graph if pp + ss not in seen}
  46. # groups.append(seen)
  47. # graph -= seen
  48. # return groups
  49. # def get_solid(walls):
  50. # xmin, *_, xmax = sorted(int(p.real) for p in walls)
  51. # ymin, *_, ymax = sorted(int(p.imag) for p in walls)
  52. # mesh = {complex(x, y) for x in range(xmin, xmax + 1) for y in range(ymin, ymax + 1)}
  53. # void = mesh - walls
  54. # groups = connected_components(void)
  55. # border = lambda p: p.real in {xmin, xmax} or p.imag in {ymin, ymax}
  56. # inside = {p for g in groups if not any(border(p) for p in g) for p in g}
  57. # solid = inside | walls
  58. # return solid
  59. # def solve1():
  60. # pos = 0
  61. # walls = {pos}
  62. # for line in text.splitlines():
  63. # d, n, _ = line.split()
  64. # dirx = 1j ** 'RDLU'.index(d)
  65. # goal = pos + dirx * int(n)
  66. # while pos != goal:
  67. # pos += dirx
  68. # walls.add(pos)
  69. # solid = get_solid(walls)
  70. # print(len(solid))
  71. # def solve2():
  72. # points = {0}
  73. # pos = 0
  74. # for line in text.splitlines():
  75. # _, _, rest = line.split()
  76. # n, d = int(rest[1:-1][1:][:5], 16), 'RDLU'[int(rest[-2])]
  77. # dirx = 1j ** 'RDLU'.index(d)
  78. # pos += dirx * int(n)
  79. # points.add(pos)
  80. # b2s = {
  81. # complex(bx, by) + dx + dy: 3 * complex(sx, sy) + dx + dy
  82. # for sx, bx in enumerate(sorted({p.real for p in points}))
  83. # for sy, by in enumerate(sorted({p.imag for p in points}))
  84. # for dx in [-1, 0, 1] for dy in [-1j, 0, 1j]
  85. # }
  86. # s2b = {v: k for k, v in b2s.items()}
  87. # pos, smallpos = 0, b2s[0]
  88. # walls = {smallpos}
  89. # for line in text.splitlines():
  90. # d, n, rest = line.split()
  91. # n, d = int(rest[1:-1][1:][:5], 16), 'RDLU'[int(rest[-2])]
  92. # dirx = 1j ** 'RDLU'.index(d)
  93. # pos += dirx * int(n)
  94. # goal = b2s[pos]
  95. # while smallpos != goal:
  96. # smallpos += dirx
  97. # walls.add(smallpos)
  98. # solid = get_solid(walls)
  99. # out = 0
  100. # for smallpos in solid:
  101. # c1, c2 = s2b[smallpos], s2b[smallpos + 1 + 1j]
  102. # w, h = (c2 - c1).real, (c2 - c1).imag
  103. # out += int(w * h)
  104. # print(out)
  105. # text = open(0).read()
  106. # solve1()
  107. # solve2()