package day13 import ( "math" "strconv" "strings" "tools" ) func getMoodMap(input []string, addYourself bool) ([]string, map[string]map[string]int) { moodMap := make(map[string]map[string]int) var people []string for _, line := range input { moodDescr := strings.Split(line, " ") person1 := moodDescr[0] person2 := moodDescr[10][:len(moodDescr[10])-1] moodSwing, _ := strconv.Atoi(moodDescr[3]) if moodDescr[2] == "lose" { moodSwing = -moodSwing } if _, ok := moodMap[person1]; !ok { moodMap[person1] = make(map[string]int) people = append(people, person1) } moodMap[person1][person2] = moodSwing } if addYourself { people = append(people, "myself") } return people, moodMap } func seatingPermutations(slice []string) (permutations [][]string) { 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 maxMood := 0 for _, v := range permutations { thisMood := 0 for i := 0; i < len(v); i++ { j := i + 1 if j == len(v) { j = 0 } thisMood += moodMap[v[i]][v[j]] thisMood += moodMap[v[j]][v[i]] } if thisMood < minMood { minMood = thisMood } if thisMood > maxMood { maxMood = thisMood } } return minMood, maxMood } func Part1(puzzle tools.AoCPuzzle) interface{} { people, moodMap := getMoodMap(puzzle.GetInputArray(), false) _, maxMood := getMinMaxMoods(seatingPermutations(people), moodMap) return maxMood } func Part2(puzzle tools.AoCPuzzle) interface{} { people, moodMap := getMoodMap(puzzle.GetInputArray(), true) _, maxMood := getMinMaxMoods(seatingPermutations(people), moodMap) return maxMood }