# Day 1 - Inverse Capthca 
####### Task 1 #######
# The captcha requires you to review a sequence of digits (your puzzle input) and find the sum of all 
# digits that match the next digit in the list. The list is circular, so the digit after the last digit
# is the first digit in the list.
#
#For example:
#
# 1122 produces a sum of 3 (1 + 2) because the first digit (1)
# matches the second digit and the third digit (2) matches the fourth digit.
# 1111 produces 4 because each digit (all 1) matches the next.
# 1234 produces 0 because no digit matches the next.
# 91212129 produces 9 because the only digit that matches the next one is the last digit, 9.


f = open('input.txt','r')
digits = f.read().strip()

# Task 1
previous = -1
count = 0
for digit in digits:
	digit = int(digit)
	if digit == previous:
		count += digit
	previous = digit

if int(digits[0]) == previous:
	count += int(digits[0])

print "Task 1:" + str(count)


######## Task 2 #######
# Now, instead of considering the next digit, it wants you to consider the digit halfway around 
# the circular list. That is, if your list contains 10 items, only include a digit in your sum 
# if the digit 10/2 = 5 steps forward matches it. Fortunately, your list has an even number of elements.

# For example:

# 1212 produces 6: the list contains 4 items, and all four digits match the digit 2 items ahead.
# 1221 produces 0, because every comparison is between a 1 and a 2.
# 123425 produces 4, because both 2s match each other, but no other digit has a match.
# 123123 produces 12.
# 12131415 produces 4.

# Task 2
count = 0
digit = -1
print len(digits)
half_len = len(digits) / 2
for i in range(0,half_len):
	digit = int(digits[i])
	if digit == int(digits[(i+half_len)]):
		count += digit*2

print "Task 2:" + str(count)


