-
Notifications
You must be signed in to change notification settings - Fork 0
/
question4_part_2.py
64 lines (46 loc) Β· 1.61 KB
/
question4_part_2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import re
lines = [line.strip() for line in list(open("input4.txt", "r").readlines())]
# print(lines)
def splitter(line):
splitted = line.split("|")
return splitted
def extract_integers(input_string):
# Define the regular expression pattern to match integers
integer_pattern = r"\b\d+\b"
# Use re.findall to find all matches in the input string
integers = re.findall(integer_pattern, input_string)
# Convert the matched strings to actual integers
integers = [int(num) for num in integers]
return integers
def finder(extracted_left, extracted_right):
count = 0
for left in extracted_left:
if left in extracted_right:
# print(f"found '{left}' in {extracted_right}")
count += 1
return count
final = 0
deck = {}
for game_number, line in enumerate(lines, start=1):
s = splitter(line)
extracted_left = extract_integers(s[0])
extracted_right = extract_integers(s[1])
# final += finder(extracted_left, extracted_right)
deck[game_number] = [(extracted_left[1:], extracted_right)]
print(deck)
game_to_play = 1
while game_to_play < len(lines):
points = [finder(i, j) for i, j in deck[game_to_play]]
# win_count = len(points)
# print("game:", deck[game_to_play])
# print("points,",points)
for i in points:
for j in range(1, i + 1):
deck[game_to_play + j].append(deck[game_to_play + j][0])
# print("appended game", game_to_play+1, "-- ", deck[game_to_play+j][0])
game_to_play += 1
score = 0
for i, j in deck.items():
print("this game", i, len(j))
score += len(j)
print(score)