Advent of Code: Day 6 Solution

First two for one!

Today was the first two for one script. The script I wrote initially for part 1 was instantly applicable to part 2 with only one small change. After neglecting my Python skills for a while, I can feel them coming back from the Advent of Code already!

As always, my code is available on Github. My input file is also available there.

The script I came up with for Day 06 was:

# Read in the input file.
with open ("input.txt", "r") as myfile:  

lineLen = 0  
positionLookup = {}

for line in data:  
    line = line.replace('\n', '')
    lineLen = len(line)
    print line
    for i in range(lineLen):
        if i not in positionLookup:
            positionLookup[i] = {line[i]: 1}
        elif line[i] not in positionLookup[i]:
            positionLookup[i][line[i]] = 1
            positionLookup[i][line[i]] += 1

string1 = []  
string2 = []  
for i in range(lineLen):  
    # Part 1
    string1.append(str(max(positionLookup[i], key=positionLookup[i].get)))
    # Part 2
    string2.append(str(min(positionLookup[i], key=positionLookup[i].get)))
print "".join(string1)  
print "".join(string2)  

The only change required for part one versus part two was the max used for part one was changed to min for part two. I'm proud of this solution since it was such an easy addition to transition from part one to part two. See you tomorrow!