Guest User

Untitled

a guest
Dec 18th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. ########################################################################
  2. #
  3. # Parse the input data and return a list of vertical veins.
  4. #
  5. # As usual, do this with a reduction, list comprehension, and a regex.
  6. #
  7. # The regex will capture horizontal and vertical veins in the same
  8. # groups, with the first group indicating whether it's a horizontal
  9. # vein (`y`) or vertical vein (`x`).
  10. # ```
  11. # r'([xy])=([0-9]+), [yx]=([0-9]+)\.\.([0-9]+)'
  12. # ```
  13. #
  14. # In the list comprehension, the result of the split will be bound
  15. # to `input_list`.
  16. #
  17. # The list comprehension will loop over the range of indices from
  18. # `1` to the length of the list, increasing by 5, to address the
  19. # start of each set of matches from the split expression. These
  20. # will be bound to the names `xy`, `static`, `first`, and `last`;
  21. # the last three will be converted to integers.
  22. #
  23. # The list comprehension will produce a list of tuples spanning
  24. # horizontally across the vein:
  25. #
  26. # If the first group (xy) is `x`, the one tuple will be:
  27. # ```
  28. # [ (static, (first, last)) ]
  29. # ```
  30. #
  31. # If the first group (xy) is `y`, the tuples will be:
  32. # ```
  33. # [
  34. # (first, (static, static)),
  35. # (first+1, (static, static)),
  36. # ...,
  37. # (last, (static, static))
  38. # ]
  39. # ```
  40. #
  41. # The list comprehension will be fed into the reduction, which will
  42. # simply join the lists together.
  43. #
  44. # Return the sorted results of the reduction.
  45.  
  46. parse_vertical_veins = lambda input_data: (
  47. sorted(
  48. reduce(
  49. operator.add
  50. ,
  51. [
  52. [
  53. (static, (first, last))
  54. ]
  55. if xy == 'x'
  56. else
  57. [
  58. (y, (static, static))
  59. for y in range(first, last+1)
  60. ]
  61. for input_list in [
  62. re.split(
  63. r'([xy])=([0-9]+), [yx]=([0-9]+)\.\.([0-9]+)',
  64. input_data
  65. )
  66. ]
  67. for i in range(1, len(input_list), 5)
  68. for xy in [input_list[i]]
  69. for static in [int(input_list[i+1])]
  70. for first in [int(input_list[i+2])]
  71. for last in [int(input_list[i+3])]
  72. ]
  73. )
  74. )
  75. )
Add Comment
Please, Sign In to add comment