-
Notifications
You must be signed in to change notification settings - Fork 0
/
DayThirteen.go
170 lines (117 loc) · 4.14 KB
/
DayThirteen.go
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package adventofcode2015
import (
"fmt"
"strconv"
"strings"
)
func getListPermutations(input []string) [][]string {
permutations := make([][]string, 0)
for i := 0; i < len(input); i++ {
remainingItems := make([]string, 0)
for j := 0; j < len(input); j++ {
if input[i] != input[j] {
remainingItems = append(remainingItems, input[j])
}
}
if len(remainingItems) > 0 {
followingPermutations := getListPermutations(remainingItems)
for k := 0; k < len(followingPermutations); k++ {
combination := make([]string, 0)
combination = append(combination, input[i])
combination = append(combination, followingPermutations[k]...)
permutations = append(permutations, combination)
}
} else {
combination := make([]string, 0)
combination = append(combination, input[i])
permutations = append(permutations, combination)
}
}
return permutations
}
func parseHappinessMap(input string) map[string]map[string]int {
happinessMap := make(map[string]map[string]int)
inputLines := strings.Split(input, "\n")
for i := 0; i < len(inputLines); i++ {
line := inputLines[i]
parts := strings.Split(line, " ")
firstName := parts[0]
secondName := parts[10][:len(parts[10])-1]
happinessValues, exists := happinessMap[firstName]
value, _ := strconv.Atoi(parts[3])
if parts[2] == "lose" {
value = value * -1
}
if !exists {
happinessValues = make(map[string]int)
happinessMap[firstName] = happinessValues
}
happinessValues[secondName] = value
}
return happinessMap
}
func getHappinessScoreForList(tableList []string, happinessMap map[string]map[string]int) int {
score := 0
for i := 0; i < len(tableList)-1; i++ {
score += happinessMap[tableList[i]][tableList[i+1]]
score += happinessMap[tableList[i+1]][tableList[i]]
}
score += happinessMap[tableList[len(tableList)-1]][tableList[0]]
score += happinessMap[tableList[0]][tableList[len(tableList)-1]]
return score
}
func DayThirteenExample() {
fmt.Println("Day 13 - Example")
people := []string{"Alice", "Bob", "Carol", "David"}
combinations := getListPermutations(people)
input := "Alice would gain 54 happiness units by sitting next to Bob.\nAlice would lose 79 happiness units by sitting next to Carol.\nAlice would lose 2 happiness units by sitting next to David.\nBob would gain 83 happiness units by sitting next to Alice.\nBob would lose 7 happiness units by sitting next to Carol.\nBob would lose 63 happiness units by sitting next to David.\nCarol would lose 62 happiness units by sitting next to Alice.\nCarol would gain 60 happiness units by sitting next to Bob.\nCarol would gain 55 happiness units by sitting next to David.\nDavid would gain 46 happiness units by sitting next to Alice.\nDavid would lose 7 happiness units by sitting next to Bob.\nDavid would gain 41 happiness units by sitting next to Carol."
happinessMap := parseHappinessMap(input)
score := 0
for i := 0; i < len(combinations); i++ {
newScore := getHappinessScoreForList(combinations[i], happinessMap)
if newScore > score {
score = newScore
}
}
fmt.Println("Score:", score)
}
func DayThirteenPartOne() {
fmt.Println("Day 13 - Part One")
input := ReadFile("day13-input.txt")
happinessMap := parseHappinessMap(input)
people := make([]string, 0)
for key, _ := range happinessMap {
people = append(people, key)
}
combinations := getListPermutations(people)
score := 0
for i := 0; i < len(combinations); i++ {
newScore := getHappinessScoreForList(combinations[i], happinessMap)
if newScore > score {
score = newScore
}
}
fmt.Println("Score:", score)
}
func DayThirteenPartTwo() {
fmt.Println("Day 13 - Part Two")
input := ReadFile("day13-input.txt")
happinessMap := parseHappinessMap(input)
people := make([]string, 0)
meMap := make(map[string]int)
for key, _ := range happinessMap {
people = append(people, key)
meMap[key] = 0
}
people = append(people, "me")
happinessMap["me"] = meMap
combinations := getListPermutations(people)
score := 0
for i := 0; i < len(combinations); i++ {
newScore := getHappinessScoreForList(combinations[i], happinessMap)
if newScore > score {
score = newScore
}
}
fmt.Println("Score:", score)
}