File tree 1 file changed +52
-0
lines changed
1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change
1
+ import itertools
2
+
3
+ def get_input ():
4
+ data = []
5
+ with open ('input' , 'r' ) as file :
6
+ for line in file :
7
+ line = line .strip ()
8
+
9
+ result = line .split (':' )
10
+ result [0 ] = int (result [0 ])
11
+ result [1 ] = list (map (int , result [1 ].strip ().split (' ' )))
12
+ data .append (result )
13
+ return data
14
+
15
+ def get_combinations (number_of_combos ):
16
+ values = ['+' , '*' , "||" ]
17
+
18
+ # Generate all combinations of '+', '*', "||"
19
+ combinations = list (itertools .product (values , repeat = number_of_combos ))
20
+ return combinations
21
+
22
+ def part_one (data ):
23
+ total_sum = 0
24
+ for value , parts in data :
25
+
26
+ combinations = get_combinations (len (parts ))
27
+
28
+ for combination in combinations :
29
+ tally = parts [0 ]
30
+ for index in range (1 , len (combination )):
31
+ if combination [index ] == '+' :
32
+ tally = tally + parts [index ]
33
+
34
+ if combination [index ] == '*' :
35
+ tally = tally * parts [index ]
36
+
37
+ if combination [index ] == '||' :
38
+ tally = int (str (tally ) + str (parts [index ]))
39
+
40
+ if tally == value :
41
+ total_sum = total_sum + value
42
+ break
43
+ return total_sum
44
+
45
+
46
+ def main ():
47
+ input = get_input ()
48
+ print (part_one (input ))
49
+
50
+
51
+ if __name__ == "__main__" :
52
+ main ()
You can’t perform that action at this time.
0 commit comments