DEV Community

Discussion on: Learning things about PHP during Advent of Code (Days 1-3)

Collapse
 
dschoemehl profile image
David Schoemehl

This was a big help for me. I had implemented part 1 in python storing every point and running a similar loop looking for each point in wire_1 in wire 2. Something like:

(I don't know how to use the markup to show this as Python code with indents.)
for segment in wire_1:
if segment in wire_2:
intersections.append(segment)

It worked great with the sample data, but took 16 minutes to find the answer with my actual problem data. This sent me down a path of trying to store line endpoints and calculate intersections, which would still be a more efficient solution from a memory perspective, but the math gets more complicated. After reading your post I started looking for alternatives and found that by using sets instead of lists in Python I had similar performance results.

intersections = set(wire_1) & set(wire_2)

Running time now is .2 sec! Thanks!