move permutations function to tools module
This commit is contained in:
parent
5952e6bc61
commit
13428cc28a
40
day09/day.go
40
day09/day.go
@ -7,9 +7,9 @@ import (
|
|||||||
"tools"
|
"tools"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getDistanceMap(input []string) ([]string, map[string]map[string]int) {
|
func getDistanceMap(input []string) ([]interface{}, map[string]map[string]int) {
|
||||||
distances := make(map[string]map[string]int)
|
distances := make(map[string]map[string]int)
|
||||||
var retCities []string
|
var retCities []interface{}
|
||||||
|
|
||||||
for _, line := range input {
|
for _, line := range input {
|
||||||
distDescr := strings.Split(line, " = ")
|
distDescr := strings.Split(line, " = ")
|
||||||
@ -31,42 +31,14 @@ func getDistanceMap(input []string) ([]string, map[string]map[string]int) {
|
|||||||
return retCities, distances
|
return retCities, distances
|
||||||
}
|
}
|
||||||
|
|
||||||
func cityPermutations(slice []string) (permutations [][]string) {
|
func getMinMaxDistances(permutations [][]interface{}, distances map[string]map[string]int) (int, int) {
|
||||||
if len(slice) == 1 {
|
|
||||||
return [][]string{slice}
|
|
||||||
} else if len(slice) == 2 {
|
|
||||||
return [][]string{
|
|
||||||
{
|
|
||||||
slice[0],
|
|
||||||
slice[1],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
slice[1],
|
|
||||||
slice[0],
|
|
||||||
},
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
for i := range slice {
|
|
||||||
newSlice := make([]string, i)
|
|
||||||
copy(newSlice, slice[:i])
|
|
||||||
newSlice = append(newSlice, slice[i+1:]...)
|
|
||||||
subPermutations := cityPermutations(newSlice)
|
|
||||||
for _, subPermutation := range subPermutations {
|
|
||||||
permutations = append(permutations, append([]string{slice[i]}, subPermutation...))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return permutations
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func getMinMaxDistances(permutations [][]string, distances map[string]map[string]int) (int, int) {
|
|
||||||
minDistance := math.MaxInt32
|
minDistance := math.MaxInt32
|
||||||
maxDistance := 0
|
maxDistance := 0
|
||||||
|
|
||||||
for _, v := range permutations {
|
for _, v := range permutations {
|
||||||
thisDistance := 0
|
thisDistance := 0
|
||||||
for i := 0; i < len(v)-1; i++ {
|
for i := 0; i < len(v)-1; i++ {
|
||||||
thisDistance += distances[v[i]][v[i+1]]
|
thisDistance += distances[v[i].(string)][v[i+1].(string)]
|
||||||
}
|
}
|
||||||
if thisDistance < minDistance {
|
if thisDistance < minDistance {
|
||||||
minDistance = thisDistance
|
minDistance = thisDistance
|
||||||
@ -81,14 +53,14 @@ func getMinMaxDistances(permutations [][]string, distances map[string]map[string
|
|||||||
|
|
||||||
func Part1(puzzle tools.AoCPuzzle) interface{} {
|
func Part1(puzzle tools.AoCPuzzle) interface{} {
|
||||||
cities, distances := getDistanceMap(puzzle.GetInputArray())
|
cities, distances := getDistanceMap(puzzle.GetInputArray())
|
||||||
minDistance, _ := getMinMaxDistances(cityPermutations(cities), distances)
|
minDistance, _ := getMinMaxDistances(tools.Permutations(cities), distances)
|
||||||
|
|
||||||
return minDistance
|
return minDistance
|
||||||
}
|
}
|
||||||
|
|
||||||
func Part2(puzzle tools.AoCPuzzle) interface{} {
|
func Part2(puzzle tools.AoCPuzzle) interface{} {
|
||||||
cities, distances := getDistanceMap(puzzle.GetInputArray())
|
cities, distances := getDistanceMap(puzzle.GetInputArray())
|
||||||
_, maxDistance := getMinMaxDistances(cityPermutations(cities), distances)
|
_, maxDistance := getMinMaxDistances(tools.Permutations(cities), distances)
|
||||||
|
|
||||||
return maxDistance
|
return maxDistance
|
||||||
}
|
}
|
||||||
|
|||||||
42
day13/day.go
42
day13/day.go
@ -7,9 +7,9 @@ import (
|
|||||||
"tools"
|
"tools"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getMoodMap(input []string, addYourself bool) ([]string, map[string]map[string]int) {
|
func getMoodMap(input []string, addYourself bool) ([]interface{}, map[string]map[string]int) {
|
||||||
moodMap := make(map[string]map[string]int)
|
moodMap := make(map[string]map[string]int)
|
||||||
var people []string
|
var people []interface{}
|
||||||
|
|
||||||
for _, line := range input {
|
for _, line := range input {
|
||||||
moodDescr := strings.Split(line, " ")
|
moodDescr := strings.Split(line, " ")
|
||||||
@ -34,35 +34,7 @@ func getMoodMap(input []string, addYourself bool) ([]string, map[string]map[stri
|
|||||||
return people, moodMap
|
return people, moodMap
|
||||||
}
|
}
|
||||||
|
|
||||||
func seatingPermutations(slice []string) (permutations [][]string) {
|
func getMinMaxMoods(permutations [][]interface{}, moodMap map[string]map[string]int) (int, int) {
|
||||||
if len(slice) == 1 {
|
|
||||||
return [][]string{slice}
|
|
||||||
} else if len(slice) == 2 {
|
|
||||||
return [][]string{
|
|
||||||
{
|
|
||||||
slice[0],
|
|
||||||
slice[1],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
slice[1],
|
|
||||||
slice[0],
|
|
||||||
},
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
for i := range slice {
|
|
||||||
newSlice := make([]string, i)
|
|
||||||
copy(newSlice, slice[:i])
|
|
||||||
newSlice = append(newSlice, slice[i+1:]...)
|
|
||||||
subPermutations := seatingPermutations(newSlice)
|
|
||||||
for _, subPermutation := range subPermutations {
|
|
||||||
permutations = append(permutations, append([]string{slice[i]}, subPermutation...))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return permutations
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func getMinMaxMoods(permutations [][]string, moodMap map[string]map[string]int) (int, int) {
|
|
||||||
minMood := math.MaxInt32
|
minMood := math.MaxInt32
|
||||||
maxMood := 0
|
maxMood := 0
|
||||||
|
|
||||||
@ -73,8 +45,8 @@ func getMinMaxMoods(permutations [][]string, moodMap map[string]map[string]int)
|
|||||||
if j == len(v) {
|
if j == len(v) {
|
||||||
j = 0
|
j = 0
|
||||||
}
|
}
|
||||||
thisMood += moodMap[v[i]][v[j]]
|
thisMood += moodMap[v[i].(string)][v[j].(string)]
|
||||||
thisMood += moodMap[v[j]][v[i]]
|
thisMood += moodMap[v[j].(string)][v[i].(string)]
|
||||||
}
|
}
|
||||||
if thisMood < minMood {
|
if thisMood < minMood {
|
||||||
minMood = thisMood
|
minMood = thisMood
|
||||||
@ -89,14 +61,14 @@ func getMinMaxMoods(permutations [][]string, moodMap map[string]map[string]int)
|
|||||||
|
|
||||||
func Part1(puzzle tools.AoCPuzzle) interface{} {
|
func Part1(puzzle tools.AoCPuzzle) interface{} {
|
||||||
people, moodMap := getMoodMap(puzzle.GetInputArray(), false)
|
people, moodMap := getMoodMap(puzzle.GetInputArray(), false)
|
||||||
_, maxMood := getMinMaxMoods(seatingPermutations(people), moodMap)
|
_, maxMood := getMinMaxMoods(tools.Permutations(people), moodMap)
|
||||||
|
|
||||||
return maxMood
|
return maxMood
|
||||||
}
|
}
|
||||||
|
|
||||||
func Part2(puzzle tools.AoCPuzzle) interface{} {
|
func Part2(puzzle tools.AoCPuzzle) interface{} {
|
||||||
people, moodMap := getMoodMap(puzzle.GetInputArray(), true)
|
people, moodMap := getMoodMap(puzzle.GetInputArray(), true)
|
||||||
_, maxMood := getMinMaxMoods(seatingPermutations(people), moodMap)
|
_, maxMood := getMinMaxMoods(tools.Permutations(people), moodMap)
|
||||||
|
|
||||||
return maxMood
|
return maxMood
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user