Created
October 23, 2021 06:07
-
-
Save arran4/55f4be28423615464c8ff26b5349a487 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 weights := 1; weights < 6; weights++ { | |
f(make([]float64, 0, weights), 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 { | |
for _, ee := range totals[e] { | |
ssa := []string{} | |
for _, eee := range ee { | |
sss := fmt.Sprintf("%.1f", eee) | |
ssa = append(ssa, sss) | |
} | |
fmt.Printf("%.1f = 2 x %.1f = %s\n", e * 2, e, strings.Join(ssa, " + ")) | |
} | |
} | |
} | |
func f(s []float64, p int) bool { | |
if cap(s) - len(s) <= 0 { | |
sum := 0.0 | |
for _, v := range s { | |
sum += v | |
} | |
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