File: issue69507.go

package info (click to toggle)
golang-1.24 1.24.4-3
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid
  • size: 167,820 kB
  • sloc: asm: 154,901; ansic: 7,009; sh: 2,267; javascript: 1,705; perl: 1,052; python: 421; makefile: 110; cpp: 39; f90: 8; awk: 7; objc: 4
file content (133 lines) | stat: -rw-r--r-- 2,220 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// run

// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

func main() {
	err := run()
	if err != nil {
		panic(err)
	}
}

func run() error {
	methods := "AB"

	type node struct {
		tag     string
		choices []string
	}
	all := []node{
		{"000", permutations(methods)},
	}

	next := 1
	for len(all) > 0 {
		cur := all[0]
		k := copy(all, all[1:])
		all = all[:k]

		if len(cur.choices) == 1 {
			continue
		}

		var bestM map[byte][]string
		bMax := len(cur.choices) + 1
		bMin := -1
		for sel := range selections(methods) {
			m := make(map[byte][]string)
			for _, order := range cur.choices {
				x := findFirstMatch(order, sel)
				m[x] = append(m[x], order)
			}

			min := len(cur.choices) + 1
			max := -1
			for _, v := range m {
				if len(v) < min {
					min = len(v)
				}
				if len(v) > max {
					max = len(v)
				}
			}
			if max < bMax || (max == bMax && min > bMin) {
				bestM = m
				bMin = min
				bMax = max
			}
		}

		if bMax == len(cur.choices) {
			continue
		}

		cc := Keys(bestM)
		for c := range cc {
			choices := bestM[c]
			next++

			switch c {
			case 'A':
			case 'B':
			default:
				panic("unexpected selector type " + string(c))
			}
			all = append(all, node{"", choices})
		}
	}
	return nil
}

func permutations(s string) []string {
	if len(s) <= 1 {
		return []string{s}
	}

	var result []string
	for i, char := range s {
		rest := s[:i] + s[i+1:]
		for _, perm := range permutations(rest) {
			result = append(result, string(char)+perm)
		}
	}
	return result
}

type Seq[V any] func(yield func(V) bool)

func selections(s string) Seq[string] {
	return func(yield func(string) bool) {
		for bits := 1; bits < 1<<len(s); bits++ {
			var choice string
			for j, char := range s {
				if bits&(1<<j) != 0 {
					choice += string(char)
				}
			}
			if !yield(choice) {
				break
			}
		}
	}
}

func findFirstMatch(order, sel string) byte {
	for _, c := range order {
		return byte(c)
	}
	return 0
}

func Keys[Map ~map[K]V, K comparable, V any](m Map) Seq[K] {
	return func(yield func(K) bool) {
		for k := range m {
			if !yield(k) {
				return
			}
		}
	}
}