Created
October 23, 2021 05:47
-
-
Save arran4/27d9821c79694cd84dd1fb6982230ca5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"sort" | |
"strings" | |
) | |
var ( | |
ws = []float64 { | |
2.5, | |
5, | |
7.5, | |
10, | |
} | |
totals = map[float64][][]float64{} | |
) | |
func main() { | |
for l2 := range ws[1:] { | |
l := l2 + 2 | |
f(make([]float64, 0, l), 0) | |
} | |
l := []float64{} | |
for v := range totals { | |
skip := false | |
for _, v2 := range ws { | |
if v == v2 { | |
skip = true | |
} | |
} | |
if skip { | |
continue | |
} | |
l = append(l, v) | |
} | |
sort.Sort(sort.Reverse(sort.Float64Slice(l))) | |
for _, e := range l { | |
//sa := []string{} | |
for _, ee := range totals[e] { | |
ssa := []string{} | |
for _, eee := range ee { | |
sss := fmt.Sprintf("%.1f", eee) | |
ssa = append(ssa, sss) | |
} | |
//sa = append(sa, strings.Join(ssa, " + ")) | |
fmt.Printf("%.1f = 2 x %.1f = %s\n", e * 2, e, strings.Join(ssa, " + ")) | |
} | |
//s := strings.Join(sa, " OR ") | |
//fmt.Printf("%.1f = %s\n", e, s) | |
} | |
} | |
func f(s []float64, p int) bool { | |
if cap(s) - len(s) <= 0 { | |
sum := 0.0 | |
for _, v := range s { | |
sum += v | |
} | |
//fmt.Printf("%.1f = %v\n", sum, s) | |
totals[sum] = append(totals[sum], s) | |
return true | |
} | |
for i, w := range ws[p:] { | |
r := make([]float64, len(s), cap(s)) | |
copy(r, s) | |
r = append(r, w) | |
if !f(r, i+p+1) { | |
r = append(r, w) | |
f(r, i+p+1) | |
} | |
} | |
return false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment