Beispiel #1
0
func ExampleCharacters() {
	// Permutation of character slice
	toad := permutation.New(stringSlice{'t', 'o', 'a', 'd'})

	for ok, seq := true, toad.Current(); ok; ok, seq = toad.Next(), toad.Current() {
		fmt.Println("Permutation := ", seq)
	}
	// Output:
	// Permutation :=  adot
	// Permutation :=  adto
	// Permutation :=  aodt
	// Permutation :=  aotd
	// Permutation :=  atdo
	// Permutation :=  atod
	// Permutation :=  daot
	// Permutation :=  dato
	// Permutation :=  doat
	// Permutation :=  dota
	// Permutation :=  dtao
	// Permutation :=  dtoa
	// Permutation :=  oadt
	// Permutation :=  oatd
	// Permutation :=  odat
	// Permutation :=  odta
	// Permutation :=  otad
	// Permutation :=  otda
	// Permutation :=  tado
	// Permutation :=  taod
	// Permutation :=  tdao
	// Permutation :=  tdoa
	// Permutation :=  toad
	// Permutation :=  toda
}
Beispiel #2
0
// Now try it out
func main() {

	// Permutation of simple slice
	perm := permutation.New(byteSlice{3, 5, 1})

	// How you iterate over all permutations
	for ok, seq := true, perm.Current(); ok; ok, seq = perm.Next(), perm.Current() {
		fmt.Println("Permutation := ", seq)
	}

	// Permutation of character slice
	toad := permutation.New(stringSlice{'t', 'o', 'a', 'd'})

	for ok, seq := true, toad.Current(); ok; ok, seq = toad.Next(), toad.Current() {
		fmt.Println("Permutation := ", seq)
	}
}
Beispiel #3
0
func Run(input string) int {
	b := bytes.NewBufferString(input)
	scanner := bufio.NewScanner(b)

	routes := Routes{}
	locations := Locations{}
	for scanner.Scan() {
		text := scanner.Text()
		parsed := parse(text)

		locations.AddLocation(parsed[0])
		locations.AddLocation(parsed[1])

		distance, _ := strconv.Atoi(parsed[2])

		routes.AddRoute(parsed[0], parsed[1], distance)
	}

	//Get locations in a map
	locationsMap := make(map[int]string)
	counter := 0
	for _, locn := range locations.Locations {
		locationsMap[counter] = locn
		counter++
	}

	//create a n int list
	keys := intSlice{}
	for i := 0; i < len(locationsMap); i++ {
		keys = append(keys, i)
	}

	// Permutation of simple slice
	perm := permutation.New(keys)
	shortest := 1000000

	// iterate over all permutations
	for ok, seq := true, perm.Current(); ok; ok, seq = perm.Next(), perm.Current() {

		indexes := seqToSlice(seq)

		newLocations := []string{}
		for _, index := range indexes {
			newLocations = append(newLocations, locations.Locations[index])
		}

		distance := getDistance(newLocations, routes)
		if distance == 0 {
			continue ///  Strange...
		}

		if distance < shortest {
			shortest = distance
		}
	}
	return shortest
}
Beispiel #4
0
func Run(input string, extraGuest bool) int {
	b := bytes.NewBufferString(input)
	scanner := bufio.NewScanner(b)

	relationships := Relationships{}
	persons := Persons{}
	for scanner.Scan() {

		text := scanner.Text()
		parsed := parse(text)

		persons.AddPerson(parsed[0])
		persons.AddPerson(parsed[1])

		happiness, _ := strconv.Atoi(parsed[2])
		relationships.addRelationship(parsed[0], parsed[1], happiness)

		if extraGuest == true {
			//Add an extra person
			persons.AddPerson("Guest")
		}
	}

	//Get persons in a map
	relationshipsMap := make(map[int]string)
	counter := 0
	for _, person := range persons.Persons {
		relationshipsMap[counter] = person
		counter++
	}

	//create a n int list
	keys := intSlice{}
	for i := 0; i < len(relationshipsMap); i++ {
		keys = append(keys, i)
	}

	// Permutation of simple slice
	perm := permutation.New(keys)

	happiest := 0
	// iterate over all permutations
	for ok, seq := true, perm.Current(); ok; ok, seq = perm.Next(), perm.Current() {

		indexes := seqToSlice(seq)
		newPersons := []string{}
		for _, index := range indexes {
			newPersons = append(newPersons, persons.Persons[index])
		}

		happiness := getHappiness(newPersons, relationships)
		if happiness > happiest {
			happiest = happiness
		}
	}
	return happiest
}
Beispiel #5
0
func ExampleBytes() {
	// Permutation of simple slice
	perm := permutation.New(byteSlice{3, 5, 1})

	// How you iterate over all permutations
	for ok, seq := true, perm.Current(); ok; ok, seq = perm.Next(), perm.Current() {
		fmt.Println("Permutation := ", seq)
	}
	// Output:
	// Permutation :=  [1 3 5]
	// Permutation :=  [1 5 3]
	// Permutation :=  [3 1 5]
	// Permutation :=  [3 5 1]
	// Permutation :=  [5 1 3]
	// Permutation :=  [5 3 1]
}