From 8a74628338bd26b6644463972a23bf2106ff961a Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Sun, 27 Dec 2020 06:40:20 +0100 Subject: [PATCH] day13 - almost the same as day09 :/ --- day13/day.go | 102 +++++++++++++++++++++++++++++++++++++++++++++++++ inputs/13 | 56 +++++++++++++++++++++++++++ inputs/13_test | 12 ++++++ main.go | 2 + 4 files changed, 172 insertions(+) create mode 100644 day13/day.go create mode 100644 inputs/13 create mode 100644 inputs/13_test diff --git a/day13/day.go b/day13/day.go new file mode 100644 index 0000000..4bbe60f --- /dev/null +++ b/day13/day.go @@ -0,0 +1,102 @@ +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 +} diff --git a/inputs/13 b/inputs/13 new file mode 100644 index 0000000..2d5bea2 --- /dev/null +++ b/inputs/13 @@ -0,0 +1,56 @@ +Alice would lose 2 happiness units by sitting next to Bob. +Alice would lose 62 happiness units by sitting next to Carol. +Alice would gain 65 happiness units by sitting next to David. +Alice would gain 21 happiness units by sitting next to Eric. +Alice would lose 81 happiness units by sitting next to Frank. +Alice would lose 4 happiness units by sitting next to George. +Alice would lose 80 happiness units by sitting next to Mallory. +Bob would gain 93 happiness units by sitting next to Alice. +Bob would gain 19 happiness units by sitting next to Carol. +Bob would gain 5 happiness units by sitting next to David. +Bob would gain 49 happiness units by sitting next to Eric. +Bob would gain 68 happiness units by sitting next to Frank. +Bob would gain 23 happiness units by sitting next to George. +Bob would gain 29 happiness units by sitting next to Mallory. +Carol would lose 54 happiness units by sitting next to Alice. +Carol would lose 70 happiness units by sitting next to Bob. +Carol would lose 37 happiness units by sitting next to David. +Carol would lose 46 happiness units by sitting next to Eric. +Carol would gain 33 happiness units by sitting next to Frank. +Carol would lose 35 happiness units by sitting next to George. +Carol would gain 10 happiness units by sitting next to Mallory. +David would gain 43 happiness units by sitting next to Alice. +David would lose 96 happiness units by sitting next to Bob. +David would lose 53 happiness units by sitting next to Carol. +David would lose 30 happiness units by sitting next to Eric. +David would lose 12 happiness units by sitting next to Frank. +David would gain 75 happiness units by sitting next to George. +David would lose 20 happiness units by sitting next to Mallory. +Eric would gain 8 happiness units by sitting next to Alice. +Eric would lose 89 happiness units by sitting next to Bob. +Eric would lose 69 happiness units by sitting next to Carol. +Eric would lose 34 happiness units by sitting next to David. +Eric would gain 95 happiness units by sitting next to Frank. +Eric would gain 34 happiness units by sitting next to George. +Eric would lose 99 happiness units by sitting next to Mallory. +Frank would lose 97 happiness units by sitting next to Alice. +Frank would gain 6 happiness units by sitting next to Bob. +Frank would lose 9 happiness units by sitting next to Carol. +Frank would gain 56 happiness units by sitting next to David. +Frank would lose 17 happiness units by sitting next to Eric. +Frank would gain 18 happiness units by sitting next to George. +Frank would lose 56 happiness units by sitting next to Mallory. +George would gain 45 happiness units by sitting next to Alice. +George would gain 76 happiness units by sitting next to Bob. +George would gain 63 happiness units by sitting next to Carol. +George would gain 54 happiness units by sitting next to David. +George would gain 54 happiness units by sitting next to Eric. +George would gain 30 happiness units by sitting next to Frank. +George would gain 7 happiness units by sitting next to Mallory. +Mallory would gain 31 happiness units by sitting next to Alice. +Mallory would lose 32 happiness units by sitting next to Bob. +Mallory would gain 95 happiness units by sitting next to Carol. +Mallory would gain 91 happiness units by sitting next to David. +Mallory would lose 66 happiness units by sitting next to Eric. +Mallory would lose 75 happiness units by sitting next to Frank. +Mallory would lose 99 happiness units by sitting next to George. \ No newline at end of file diff --git a/inputs/13_test b/inputs/13_test new file mode 100644 index 0000000..0f7abc0 --- /dev/null +++ b/inputs/13_test @@ -0,0 +1,12 @@ +Alice would gain 54 happiness units by sitting next to Bob. +Alice would lose 79 happiness units by sitting next to Carol. +Alice would lose 2 happiness units by sitting next to David. +Bob would gain 83 happiness units by sitting next to Alice. +Bob would lose 7 happiness units by sitting next to Carol. +Bob would lose 63 happiness units by sitting next to David. +Carol would lose 62 happiness units by sitting next to Alice. +Carol would gain 60 happiness units by sitting next to Bob. +Carol would gain 55 happiness units by sitting next to David. +David would gain 46 happiness units by sitting next to Alice. +David would lose 7 happiness units by sitting next to Bob. +David would gain 41 happiness units by sitting next to Carol. \ No newline at end of file diff --git a/main.go b/main.go index 902cc58..f21ddb6 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,7 @@ import ( "aoc2015/day10" "aoc2015/day11" "aoc2015/day12" + "aoc2015/day13" "flag" "fmt" "os" @@ -42,6 +43,7 @@ func initDayFunctions() { 10: {1: day10.Part1, 2: day10.Part2}, 11: {1: day11.Part1, 2: day11.Part2}, 12: {1: day12.Part1, 2: day12.Part2}, + 13: {1: day13.Part1, 2: day13.Part2}, } }