Today’s challenge is here. Here we get a punch of lines in the form of two points and we have to find number of points where they cross. For parsing the data I used a regular expression (\d+),(\d+) -> (\d+),(\d+).
Loading data from the input file:
Task 1
In this task we have to find number of points where lines cross, but we have to skip the diagonal lines. When I was solving this task I did a mistake and I included the logic for counting the diagonal lines as well.
Here is the function for generating the list of points for any line. I used generators pretty liberally as well as the itertools.repeat function, which is used to generate infinite sequences of a single value. I had to use that for cases where the line is vertical or horizontal, because otherwise the zip function would stop at the first iteration.
Here we use two sets to store the visited points as well as the points where lines cross. We can do this because we don’t need to know how many lines cross at any given point, just the number of crossing points in general. A set is a perfect choice for this, it saves us memory and time.