Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ########################################################################
- #
- # Parse the input data and return a list of vertical veins.
- #
- # As usual, do this with a reduction, list comprehension, and a regex.
- #
- # The regex will capture horizontal and vertical veins in the same
- # groups, with the first group indicating whether it's a horizontal
- # vein (`y`) or vertical vein (`x`).
- # ```
- # r'([xy])=([0-9]+), [yx]=([0-9]+)\.\.([0-9]+)'
- # ```
- #
- # In the list comprehension, the result of the split will be bound
- # to `input_list`.
- #
- # The list comprehension will loop over the range of indices from
- # `1` to the length of the list, increasing by 5, to address the
- # start of each set of matches from the split expression. These
- # will be bound to the names `xy`, `static`, `first`, and `last`;
- # the last three will be converted to integers.
- #
- # The list comprehension will produce a list of tuples spanning
- # horizontally across the vein:
- #
- # If the first group (xy) is `x`, the one tuple will be:
- # ```
- # [ (static, (first, last)) ]
- # ```
- #
- # If the first group (xy) is `y`, the tuples will be:
- # ```
- # [
- # (first, (static, static)),
- # (first+1, (static, static)),
- # ...,
- # (last, (static, static))
- # ]
- # ```
- #
- # The list comprehension will be fed into the reduction, which will
- # simply join the lists together.
- #
- # Return the sorted results of the reduction.
- parse_vertical_veins = lambda input_data: (
- sorted(
- reduce(
- operator.add
- ,
- [
- [
- (static, (first, last))
- ]
- if xy == 'x'
- else
- [
- (y, (static, static))
- for y in range(first, last+1)
- ]
- for input_list in [
- re.split(
- r'([xy])=([0-9]+), [yx]=([0-9]+)\.\.([0-9]+)',
- input_data
- )
- ]
- for i in range(1, len(input_list), 5)
- for xy in [input_list[i]]
- for static in [int(input_list[i+1])]
- for first in [int(input_list[i+2])]
- for last in [int(input_list[i+3])]
- ]
- )
- )
- )
Add Comment
Please, Sign In to add comment