File tree Expand file tree Collapse file tree 1 file changed +22
-15
lines changed Expand file tree Collapse file tree 1 file changed +22
-15
lines changed Original file line number Diff line number Diff line change @@ -27,34 +27,41 @@ func main() {
27
27
scan := bufio .NewScanner (file )
28
28
29
29
var parts []string
30
- var sum , result int
30
+ var result , sum int
31
31
var numbers []int
32
32
33
33
for scan .Scan () {
34
34
parts = strings .SplitN (scan .Text (), ": " , 2 )
35
35
sum , _ = strconv .Atoi (parts [0 ])
36
36
numbers = util .StringToInts (parts [1 ], " " )
37
37
38
- possibilities := int ( math . Pow ( 2 , float64 ( len ( numbers )- 1 )))
39
- for i := range possibilities {
40
- bin := ( "0000000000" + fmt . Sprintf ( "%b" , i ))
41
- bin = bin [ len ( bin ) - len ( numbers ) + 1 :]
38
+ if resolves ( sum , numbers ) {
39
+ result += sum
40
+ }
41
+ }
42
42
43
- total := numbers [0 ]
44
- for k := range len (bin ) {
45
- total = modifiers [bin [k ]- '0' ](total , numbers [k + 1 ])
43
+ fmt .Println (result )
44
+ }
46
45
47
- if total > sum {
48
- break
49
- }
50
- }
46
+ func resolves (sum int , numbers []int ) bool {
47
+ possibilities := int (math .Pow (2 , float64 (len (numbers )- 1 )))
48
+ for i := range possibilities {
49
+ bin := ("0000000000" + fmt .Sprintf ("%b" , i ))
50
+ bin = bin [len (bin )- len (numbers )+ 1 :]
51
51
52
- if total == sum {
53
- result += total
52
+ total := numbers [0 ]
53
+ for k := range len (bin ) {
54
+ total = modifiers [bin [k ]- '0' ](total , numbers [k + 1 ])
55
+
56
+ if total > sum {
54
57
break
55
58
}
56
59
}
60
+
61
+ if total == sum {
62
+ return true
63
+ }
57
64
}
58
65
59
- fmt . Println ( result )
66
+ return false
60
67
}
You can’t perform that action at this time.
0 commit comments