day 4: apparently I don't know scratchcards
Some checks are pending
Build nvim / Build-Packages (push) Waiting to run
Some checks are pending
Build nvim / Build-Packages (push) Waiting to run
This commit is contained in:
34
4/script.py
Normal file
34
4/script.py
Normal file
@@ -0,0 +1,34 @@
|
||||
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(" ")
|
||||
return winning_numbers, your_numbers
|
||||
|
||||
|
||||
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 line in inputfile:
|
||||
winning_numbers, your_numbers = parseline(line)
|
||||
value = get_point_value_for_line(winning_numbers, your_numbers)
|
||||
values.append(value)
|
||||
return sum(values)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print(get_lines("input"))
|
||||
|
||||
Reference in New Issue
Block a user