DEV Community

Discussion on: Advent of Code 2021: Day 05 with Python, even more numpy

Collapse
 
meseta profile image
Yuan Gao

the '+' does an addition, admittedly that should have been an | instead to be more clear. I have edited the article

coords[:, 0] == coords[:, 2] generates a list that is True if the x values are the same and False otherwise
coords[:, 1] == coords[:, 3] generates a list that is True if the y values are the same and False otherwise

so (coords[:, 0] == coords[:, 2]) + (coords[:, 1] == coords[:, 3]) adds those together, so the result is True if either of the two conditions are true. I should have used | to do an OR operation instead. Though the result is the same.

Longer code could look like:

x_same_indexes = coords[:, 0] == coords[:, 2]
y_same_indexes = coords[:, 1] == coords[:, 3]

hv_indexes = x_same_indexes | y_same_indexes 
hv = coords[hv_indexes]
Enter fullscreen mode Exit fullscreen mode