def convert_seat_to_seat_id(seat): '''The seat as a seat id, e.g. row 44 column 5 is seat id 357 (row + (column * 8))''' return seat['row'] * 8 + seat['column'] def convert_binary_to_decimal(binary, one_char): '''Converts a binary number to decimal, with a specified character for 1, so FBF with B as 1 gives 3''' decimal = 0 # https://www.w3schools.com/python/python_howto_reverse_string.asp for index, bit in enumerate(binary[::-1]): if (bit == one_char): decimal += (2**index + 1) - 1 return decimal def highest_seat_id_from_boarding_passes(boarding_passes): '''The highest seat id from a list of boarding passes''' boarding_pass_with_highest_seat_id = max( boarding_passes, key=lambda boarding_pass: convert_seat_to_seat_id( convert_boarding_pass_to_seat(boarding_pass))) return convert_seat_to_seat_id( convert_boarding_pass_to_seat(boarding_pass_with_highest_seat_id)) if __name__ == '__main__': lines = read_input_file.read( '/Users/jondarrer/Code/advent-of-code-2020/src/input/day5.txt') print(highest_seat_id_from_boarding_passes(lines))
import read_input_file def is_password_valid(min, max, char, password): count = password.count(char) if (min > count or max < count): return False return True def number_of_valid_passwords(list_of_password_and_policy): valids = 0 for password_and_policy in list_of_password_and_policy: if (is_password_valid(int(password_and_policy['min']), int(password_and_policy['max']), password_and_policy['char'], password_and_policy['password']) == True): valids = valids + 1 return valids if __name__ == '__main__': list_of_password_and_policy = read_input_file.read( '/Users/jondarrer/Code/advent-of-code-2020/src/input/day2.txt') print(number_of_valid_passwords(list_of_password_and_policy))