This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def read_input(): | |
# Read in an input file containing ones and zeros on each line. | |
with open('./input/3_1.txt') as f: | |
input = f.readlines() | |
# Convert each line to a list of integers. | |
input = [list(map(int, line.strip())) for line in input] | |
return input | |
def calculate_most_common_number(input, i): | |
# For each list in the input list, get element i | |
subset = [row[i] for row in input] | |
# If there is an equal number of 0's and 1's, return 1. | |
if subset.count(0) == subset.count(1): | |
return 1 | |
# Get the most common number in the subset. | |
most_common_number = max(set(subset), key=subset.count) | |
return most_common_number | |
def filter_list_based_on_most_common_number(input, i): | |
# Calculate the most common number at position i in each list in input. | |
most_common_number = calculate_most_common_number(input, i) | |
# Filter out all lists that do not contain the most common number at position i. | |
filtered_list = [row for row in input if row[i] == most_common_number] | |
return filtered_list | |
def get_unique_based_on_most_common_filtering(input): | |
# Get the unique number based on the most common number filtering. | |
unique_number = 0 | |
for i in range(len(input[0])): | |
filtered_list = filter_list_based_on_most_common_number(input, i) | |
# put the filtered list back in the input variable. | |
input = filtered_list | |
# if length of filtered list is 1, then the unique number is the number at position i in the filtered list. | |
if len(filtered_list) == 1: | |
# convert the list of ones and zeros to string and then to an binary integer. | |
unique_number += int(''.join(map(str, filtered_list[0])), 2) | |
# break | |
break | |
return unique_number | |
def calculate_least_common_number(input, i): | |
# For each list in the input list, get element i | |
subset = [row[i] for row in input] | |
# If there is an equal number of 0's and 1's, return 1. | |
if subset.count(0) == subset.count(1): | |
return 0 | |
# Get the least common number in the subset. | |
least_common_number = min(set(subset), key=subset.count) | |
return least_common_number | |
def filter_list_based_on_least_common_number(input, i): | |
# Calculate the least common number at position i in each list in input. | |
least_common_number = calculate_least_common_number(input, i) | |
# Filter out all lists that do not contain the least common number at position i. | |
filtered_list = [row for row in input if row[i] == least_common_number] | |
return filtered_list | |
def get_unique_based_on_least_common_filtering(input): | |
# Get the unique number based on the least common number filtering. | |
unique_number = 0 | |
for i in range(len(input[0])): | |
filtered_list = filter_list_based_on_least_common_number(input, i) | |
# put the filtered list back in the input variable. | |
input = filtered_list | |
# if length of filtered list is 1, then the unique number is the number at position i in the filtered list. | |
if len(filtered_list) == 1: | |
# convert the list of ones and zeros to string and then to an binary integer. | |
unique_number += int(''.join(map(str, filtered_list[0])), 2) | |
# break | |
break | |
return unique_number | |
def main(): | |
# calculate both the unique numbers based on most common and least common filtering. | |
input = read_input() | |
unique_number_most_common = get_unique_based_on_most_common_filtering(input) | |
unique_number_least_common = get_unique_based_on_least_common_filtering(input) | |
print('The unique number based on most common filtering is: {}'.format(unique_number_most_common)) | |
print('The unique number based on least common filtering is: {}'.format(unique_number_least_common)) | |
# Multiply them together and print the result. | |
print('The product of the two unique numbers is: {}'.format(unique_number_most_common * unique_number_least_common)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment