day 03
squares with three sides
approach
every line becomes a tuple of three integers. a triple is a valid triangle when each pair of sides adds up to more than the third, checked as three explicit comparisons.
part 1 counts the rows that pass. part 2 reads the triangles down the columns instead: the rows are cut into chunks of three, and for each column index the three values in the chunk are tested as one triangle.
the trick
part 2 doesn't re-parse anything: it takes three rows at a time and picks the same column out of each.
python
def chunks(arr, k):
for i in range(0, len(arr), k):
yield arr[i:i + k]
def part2(triangles, k):
res = 0
for i in range(k):
for ch in chunks(triangles, k):
res += are_triangle(*(c[i] for c in ch))
return res