Skip to content
advent of code 2017

day 01

inverse captcha

approach

the input line becomes a list of digits. a small match helper compares the digit at i with the one k places ahead, wrapping around the end with % n.

part 1 sums every digit that matches its immediate neighbour (k = 1). part 2 reuses the same helper with k set to half the list length.

the trick

one comparison with a configurable offset and a modulo covers both parts and the wrap-around.

python
def match(arr, i, k = 1):
    n = len(arr)
    return arr[i] == arr[(i + k) % n]