From e199c391c5db203436aeedc64dac0fe0d15c23f9 Mon Sep 17 00:00:00 2001 From: Jaime9070 Date: Mon, 29 Jan 2024 13:55:43 +0100 Subject: [PATCH] day 4 --- python/Day 4/main.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 python/Day 4/main.py diff --git a/python/Day 4/main.py b/python/Day 4/main.py new file mode 100644 index 0000000..ee24415 --- /dev/null +++ b/python/Day 4/main.py @@ -0,0 +1,35 @@ +from io import open + +# We open the txt document containing the games +input=open('input.txt','r') +# We read the text inside distinguishing by lines +lines = input.readlines() +# Close the txt file to be able to work on the text without having the file open. +input.close() +del(input) + +games_id=[] # number of the game +wining_numbers=[] # Collect of winning numbers +game_numbers=[] # Game numbers of the scratchcards +for line in lines: + game=line.replace("Card ","").split(":") + games_id.append(int(game[0].strip())) + numbers=game[1] + game=numbers.split("|") + wining_numbers.append(list(map(int,game[0].strip().split()))) + game_numbers.append(list(map(int,game[1].strip().split()))) + +# We check that the winning numbers are on the scratchcard numbers and accumulate the points. +points=[] +for game in games_id: + points.append(0) + for number in wining_numbers[game-1]: + if number in game_numbers[game-1] and points[game-1]>=1: + points[game-1]=points[game-1]*2 + elif number in game_numbers[game-1]: + points[game-1]+=1 + +print(sum(points)) + + + \ No newline at end of file