-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
183 lines (159 loc) · 2.94 KB
/
main.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
174
175
176
177
178
179
180
181
182
183
package main
import (
"bufio"
"io"
"strconv"
"strings"
)
func fs1(input io.Reader) (int, error) {
scanner := bufio.NewScanner(input)
wishes := make(map[string]map[string]int)
for scanner.Scan() {
s := scanner.Text()
idx := indexAll(s, " ")
from := s[:idx[0]]
gain := true
if s[idx[1]+1] == 'l' {
gain = false
}
v, err := strconv.Atoi(s[idx[2]+1 : idx[3]])
if err != nil {
return 0, err
}
if !gain {
v = -v
}
to := s[idx[9]+1 : len(s)-1]
_, exists := wishes[from]
if !exists {
wishes[from] = make(map[string]int)
}
wishes[from][to] = v
}
seat := make(map[string]bool)
for k := range wishes {
seat[k] = false
}
t := Table{
table: make([]string, len(wishes)),
wishes: wishes,
seat: seat,
}
return t.findBest(), nil
}
type Table struct {
table []string
wishes map[string]map[string]int
seat map[string]bool
}
func (t *Table) findBest() int {
allSeat := true
nextSeat := 0
for i, name := range t.table {
if name == "" {
allSeat = false
nextSeat = i
}
}
if allSeat {
return t.sum()
}
best := 0
for person, seated := range t.seat {
if seated {
continue
}
t.table[nextSeat] = person
t.seat[person] = true
best = max(best, t.findBest())
t.table[nextSeat] = ""
t.seat[person] = false
}
return best
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func (t *Table) sum() int {
sum := 0
for i := 0; i < len(t.table); i++ {
from := t.table[i]
left := t.left(i)
right := t.right(i)
sum += t.wishes[from][left] + t.wishes[from][right]
}
return sum
}
func (t *Table) left(i int) string {
if i == 0 {
return t.table[len(t.table)-1]
}
return t.table[i-1]
}
func (t *Table) right(i int) string {
if i == len(t.table)-1 {
return t.table[0]
}
return t.table[i+1]
}
const me = "me"
func fs2(input io.Reader) (int, error) {
scanner := bufio.NewScanner(input)
wishes := make(map[string]map[string]int)
persons := make(map[string]struct{})
for scanner.Scan() {
s := scanner.Text()
idx := indexAll(s, " ")
from := s[:idx[0]]
gain := true
if s[idx[1]+1] == 'l' {
gain = false
}
v, err := strconv.Atoi(s[idx[2]+1 : idx[3]])
if err != nil {
return 0, err
}
if !gain {
v = -v
}
to := s[idx[9]+1 : len(s)-1]
_, exists := wishes[from]
if !exists {
wishes[from] = make(map[string]int)
}
wishes[from][to] = v
persons[from] = struct{}{}
persons[to] = struct{}{}
}
wishes[me] = make(map[string]int)
for person := range persons {
wishes[person][me] = 0
wishes[me][person] = 0
}
seat := make(map[string]bool)
for k := range wishes {
seat[k] = false
}
t := Table{
table: make([]string, len(wishes)),
wishes: wishes,
seat: seat,
}
return t.findBest(), nil
}
func indexAll(s string, search string) []int {
i := 0
var res []int
for i < len(s) {
index := strings.Index(s[i:], search)
if index == -1 {
return res
}
res = append(res, index+i)
i += index + len(search)
}
return res
}