Day 1

Okay we're going to try and solve some of the AoC puzzles in python since it's a fun way to improve...

The goal of this one is to look at a huge list of numbers and compute a sum. The sum is the determined by two adjacent identical digits. The sequence also is circular so we must consider the first and last as touching

In [1]:
captcha_file = open("H:/python_jiggering/logs/aoc_2017/d1_input.txt") 
captcha = captcha_file.read()

# I guess we can just solve that circular problem by addin a copy of the first
# digit to the end
captcha = captcha + captcha[0]

# shorter version for testing
#captcha2 = captcha[:50]
captcha2 = captcha
# So.... I think we're going to want some regular expressions here
import re

# So... not sure how to get this to replace sequences of three so I guess
# we can just iterate a lot...
len_cap = 0
while len_cap < len(captcha2):
    len_cap = len(captcha2)
    captcha2 = re.sub(pattern = r'(.)\1', repl = r'\1*\1', string = captcha2)

I don't understand what this raw string thing is about but....whatever. Now we want to maybe loop through the string and record when we see a *

In [2]:
sum_vec = []
for i in range(len(captcha2)):
    if(captcha2[i] == '*'):
        sum_vec.append(captcha2[i-1])
# and compute the total:
total = 0
for num in sum_vec:
    total += int(num)
total
Out[2]:
1393

Part 2

Now we have a much more difficult task which is identifying which ones have a corresponding key on the other side... Actually this might be very easy. I think we can just split this in half, scroll through them, and add matching pairs

In [4]:
# reset
captcha_file = open("H:/python_jiggering/logs/aoc_2017/d1_input.txt") 
captcha3 = captcha_file.read()


halfway = int(len(captcha3)/2)
l1 = captcha[:halfway]
l2 = captcha[halfway:]
sum_vec2 = 0
for a, b in zip(l1, l2):
    if a == b:
        sum_vec2 += int(a)*2
sum_vec2
Out[4]:
1292