Skip to content
advent of code 2017

day 02

corruption checksum

approach

the input is read as a grid of integers, one row per line.

part 1 sums the difference between the largest and smallest value of each row. part 2 checks every pair in a row until it finds two numbers where one divides the other, and adds up the quotients.

the trick

checking the pair in both directions (a % b or b % a) means each unordered pair only has to be visited once.

python
def div_or_mult(a, b):
    return a % b == 0 or b % a == 0

def find_div_or_mult(row):
    n = len(row)
    for i in range(n):
        for j in range(i + 1, n):
            if div_or_mult(row[i], row[j]):
                return max(row[i] // row[j], row[j] // row[i])