-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path13.go
173 lines (150 loc) · 3.41 KB
/
13.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
171
172
173
package main
import (
"bufio"
"fmt"
"log"
"os"
"regexp"
"sort"
"strconv"
)
type inst struct {
a string
b string
happiness int
}
type key struct {
a string
b string
}
type queueItem struct {
people []string
happiness int
}
// Keys returns the keys of a map
func Keys(x map[string]int) []string {
var keys []string
for key := range x {
keys = append(keys, key)
}
sort.Strings(keys)
return keys
}
// Unique dedupliates a list
func Unique(x []string) []string {
m := make(map[string]int)
for _, v := range x {
if _, ok := m[v]; !ok {
m[v] = 0
}
}
return Keys(m)
}
// Contains checks if string y exists in []string x
func Contains(x []string, y string) bool {
for _, v := range x {
if v == y {
return true
}
}
return false
}
// Except returns list x excluding items in list y
func Except(x []string, y []string) []string {
var e []string
for _, v := range x {
if !Contains(y, v) {
e = append(e, v)
}
}
return e
}
func format(line string) inst {
re := regexp.MustCompile("([A-Za-z]+) would (gain|lose) ([0-9]+) happiness.* to ([A-Za-z]+)")
res := re.FindStringSubmatch(line)
if res == nil {
log.Fatal("Regex failed on input", line)
}
happiness, err := strconv.Atoi(res[3])
if err != nil {
log.Fatal(err)
}
if res[2] == "lose" {
happiness = 0 - happiness
}
return inst{a: res[1], b: res[4], happiness: happiness}
}
func createPlan(happyMap map[key]int, people []string) []queueItem {
var queue []queueItem
var seatingPlan []queueItem
for _, person := range people {
queue = append(queue, queueItem{people: []string{person}, happiness: 0})
}
// fill/drain the queue
for {
if len(queue) == 0 {
break
}
// pop front
var item queueItem
item, queue = queue[0], queue[1:]
// keep track of completed seating plans
if len(item.people) == len(people) {
// close the loop
happiness := happyMap[key{a: item.people[len(item.people)-1], b: item.people[0]}]
happiness += happyMap[key{b: item.people[len(item.people)-1], a: item.people[0]}]
seatingPlan = append(seatingPlan,
queueItem{people: item.people,
happiness: item.happiness + happiness})
}
person := item.people[len(item.people)-1]
for _, next := range Except(people, item.people) {
people := make([]string, len(item.people))
/* avoid references */
copy(people, item.people)
//
happiness := happyMap[key{a: person, b: next}]
happiness += happyMap[key{b: person, a: next}]
queue = append(queue, queueItem{people: append(people, next),
happiness: item.happiness + happiness})
}
}
return seatingPlan
}
func main() {
file, err := os.Open("input/13.txt")
if err != nil {
log.Fatal(err)
}
defer file.Close()
happyMap := make(map[key]int)
var people []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
inst := format(scanner.Text())
happyMap[key{a: inst.a, b: inst.b}] = inst.happiness
// add me
happyMap[key{a: "me", b: inst.b}] = 0
happyMap[key{a: inst.a, b: "me"}] = 0
people = append(people, inst.a, inst.b)
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
seatingPlan := createPlan(happyMap, Unique(people))
max := 0
for _, plan := range seatingPlan {
if plan.happiness > max {
max = plan.happiness
}
}
fmt.Println(max)
seatingPlan = createPlan(happyMap, append(Unique(people), "me"))
max = 0
for _, plan := range seatingPlan {
if plan.happiness > max {
max = plan.happiness
}
}
fmt.Println(max)
}