2017-12-01 - Inverse Captcha
(original .ipynb)
puzzle_input_str = open("puzzle_input/day1.txt").read()
def matches_next(lst, idx):
assert(idx < len(lst))
return lst[idx] == lst[idx-1]
def captcha(captcha_str):
captcha_digits = [ int(s) for s in captcha_str ]
matches = [ captcha_digits[i] for i in range(-1, len(captcha_digits)-1) if matches_next(captcha_digits, i) ]
return sum(matches)
assert(captcha("1122") == 3)
assert(captcha("1111") == 4)
assert(captcha("1234") == 0 )
assert(captcha("91212129") == 9)
print("part one:", captcha(puzzle_input_str))
part one: 1203
import math
def opposite_index(lst, i):
l = len(lst)
return (i + l + math.floor(l/2)) % l
def matches_opposite(lst, idx):
return lst[idx] == lst[opposite_index(lst, idx)]
def captcha2(captcha_str):
captcha_digits = [ int(s) for s in captcha_str ]
matches = [ captcha_digits[i] for i in range(0, len(captcha_digits)) if matches_opposite(captcha_digits, i) ]
return sum(matches)
assert(captcha2("1212") == 6)
assert(captcha2("1221") == 0)
assert(captcha2("123425") == 4)
assert(captcha2("123123") == 12)
assert(captcha2("12131415") == 4)
print("part two:", captcha2(puzzle_input_str))
part two: 1146