46 lines
1.5 KiB
Python
46 lines
1.5 KiB
Python
def parseline(line):
|
|
meaningful_input = line.split(":")[1]
|
|
input_split = meaningful_input.split("|")
|
|
winning_numbers = input_split[0].strip().split(" ")
|
|
your_numbers = input_split[1].strip().split(" ")
|
|
no_empty_win = list(filter(lambda x: x != '', winning_numbers))
|
|
no_empty_yours = list(filter(lambda x: x != '', your_numbers))
|
|
return no_empty_win, no_empty_yours
|
|
|
|
def detect_duplicates(list_of_nums):
|
|
print(list_of_nums)
|
|
return len(set(list_of_nums)) != len(list_of_nums)
|
|
|
|
def get_point_value_for_line(winning_numbers, your_numbers):
|
|
count = 0
|
|
hashmap = {}
|
|
for number in winning_numbers:
|
|
for index, y in enumerate(your_numbers):
|
|
if y == number and not hashmap.get(index):
|
|
count += 1
|
|
hashmap[index] = True
|
|
if count == 0:
|
|
return 0
|
|
return 2**(count - 1)
|
|
|
|
|
|
def get_lines(filename):
|
|
values = []
|
|
with open(filename, "r") as inputfile:
|
|
for index, line in enumerate(inputfile):
|
|
newindex = index + 1
|
|
winning_numbers, your_numbers = parseline(line)
|
|
|
|
if detect_duplicates(winning_numbers):
|
|
print(f"Card {newindex}: Winning numbers has duplicates")
|
|
if detect_duplicates(your_numbers):
|
|
print(f"Card {newindex}: your numbers has duplicates")
|
|
value = get_point_value_for_line(winning_numbers, your_numbers)
|
|
values.append(value)
|
|
return sum(values)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
print(get_lines("input"))
|
|
|