Part 1
In this puzzle, we are asked to password hidden in columns of text. Each column has a most commonly used letter. Concatenating these together for each column gives us the solution.
First we transpose our text from row-oriented to column-oriented:
columns = []
for i in range(len(lines[0])):
columns.append([line[i] for line in lines])
We then iterate over the columns, use the python Counter class to find the most common letter and concatenate them together:
def most_common(columns, position):
solution = ""
for column in columns:
c = Counter(column)
char = c.most_common()[position][0]
solution += char
return solution
Part 2
In part two, the password is found by concatenating the least commonly used letter in each column.
We reuse the function from part 1, supply the argument -1 for the position instead. In python this yields the last element of a list. The rest of the function works the same and results in the password.
No comments:
Post a Comment