File tree Expand file tree Collapse file tree 1 file changed +53
-0
lines changed Expand file tree Collapse file tree 1 file changed +53
-0
lines changed Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+
4
+ from typing import List
5
+ from itertools import product
6
+
7
+ split_data = True
8
+ completed = True
9
+ raw_data = None # Not To be touched
10
+
11
+ def part1 (data :List [str ]):
12
+ result = 0
13
+
14
+ for line in data :
15
+ output , numbers = line .split (': ' )
16
+ numbers = numbers .split (' ' )
17
+
18
+ for operators in product (['+' , '*' ], repeat = len (numbers ) - 1 ):
19
+ equation = int (numbers [0 ])
20
+ for op , num in zip (operators , numbers [1 :]):
21
+ if op == '+' :
22
+ equation += int (num )
23
+ else :
24
+ equation *= int (num )
25
+
26
+ if equation == int (output ):
27
+ result += int (output )
28
+ break
29
+
30
+ return result
31
+
32
+ def part2 (data :List [str ]):
33
+ result = 0
34
+
35
+ for line in data :
36
+ output , numbers = line .split (': ' )
37
+ numbers = numbers .split (' ' )
38
+
39
+ for operators in product (['+' , '*' , '||' ], repeat = len (numbers ) - 1 ):
40
+ equation = int (numbers [0 ])
41
+ for op , num in zip (operators , numbers [1 :]):
42
+ if op == '+' :
43
+ equation += int (num )
44
+ elif op == '*' :
45
+ equation *= int (num )
46
+ else :
47
+ equation = int (str (equation )+ num )
48
+
49
+ if equation == int (output ):
50
+ result += int (output )
51
+ break
52
+
53
+ return result
You can’t perform that action at this time.
0 commit comments