# Day 2: Corruption Checksum
from itertools import permutations
from time import perf_counter
from u.colors import cyan, purple, red

t, a, b = perf_counter(), 0, 0

for row in open('y2017/d2/i.txt').read().splitlines():
  nums = list(map(int, row.split()))
  max_v = min_v = nums[0]
  row_quotient = None

  for left, right in permutations(nums, 2):
    # Every value appears as the left-side element, so min/max can be tracked
    # for part 1 without a separate pass over the row.
    max_v = max(max_v, left)
    min_v = min(min_v, left)

    # The puzzle guarantees one evenly divisible pair per row.
    # Store it once, but keep the loop running so part 1 still finishes in the
    # same pass.
    if row_quotient is None and left % right == 0:
      row_quotient = left // right

  a += max_v - min_v
  b += row_quotient or 0

cyan(f'\na) {a}')
purple(f'b) {b}')
red(f'{(perf_counter() - t) * 1000} ms\n')
