/
day2.go
257 lines (223 loc) · 5.47 KB
/
day2.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
"go.uber.org/zap"
)
type Round struct {
Balls []Ball
}
func NewRoundFromString(sRound string) Round {
zap.L().Sugar().Debugw("Got round",
"round", sRound,
)
sBalls := strings.Split(sRound, ",")
var balls []Ball
for _, sBall := range sBalls {
ball := NewBallFromString(sBall)
zap.L().Sugar().Debugw("received ball object",
"ball", ball,
)
balls = append(balls, ball)
}
round := Round{
Balls: balls,
}
return round
}
type Ball struct {
Color string
Count int
}
func NewBallFromString(sBall string) Ball {
sBallParts := strings.Split(strings.Trim(sBall, " "), " ")
sCount, parsedColor := sBallParts[0], sBallParts[1]
parsedCount, _ := strconv.Atoi(sCount)
var ball Ball
ball.Count = parsedCount
ball.Color = parsedColor
zap.L().Sugar().Debugw("Got ball",
"sBall", sBall,
"ballPars", sBallParts,
"parsedColor", parsedColor,
"parsedCount", parsedCount,
"ball", ball,
)
return ball
}
type Game struct {
Id int
Rounds []Round
}
type GameEvaluator struct {
Balls []Ball
}
type GameMinimizer struct {
balls map[string]int
}
func (gameMinimizer *GameMinimizer) GetBallCount(color string) int {
return gameMinimizer.balls[color]
}
func (gameMinimizer *GameMinimizer) Colors() []string {
var colors []string
for key, _ := range gameMinimizer.balls {
colors = append(colors, key)
}
return colors
}
func (gameMinimizer *GameMinimizer) Minimize(game Game) {
// For each round in the game
if gameMinimizer.balls == nil {
gameMinimizer.balls = map[string]int{}
}
for _, roundInGame := range game.Rounds {
// Determine the maxSeen for each color ball in the round, store it in the game evaluator
for _, ballInRound := range roundInGame.Balls {
if ballInRound.Count > gameMinimizer.balls[ballInRound.Color] {
gameMinimizer.balls[ballInRound.Color] = ballInRound.Count
}
}
}
}
func (gameEvaluator *GameEvaluator) Evaluate(game Game) bool {
// Iterate over games, summing ball colors per round
for roundIndex, round := range game.Rounds {
perColorSums := make(map[string]int)
for _, ball := range round.Balls {
perColorSums[ball.Color] = perColorSums[ball.Color] + ball.Count
}
// For each round, compare per-color sums to evaluator balls
for _, evaluatorBall := range gameEvaluator.Balls {
// If round sum is greater than any corresponding evaluator ball, return false
if perColorSums[evaluatorBall.Color] > evaluatorBall.Count {
zap.L().Sugar().Debugw("invalid game",
"gameId", game.Id,
"color", evaluatorBall.Color,
"evaluatorCount", evaluatorBall.Count,
"roundCount", perColorSums[evaluatorBall.Color],
"roundIndex", roundIndex,
)
return false
}
}
}
zap.L().Sugar().Debugw("valid game",
"gameId", game.Id)
return true
}
func NewGameFromString(text string) Game {
// Golang regex isn't suitable for deeply nested captures
// Going with string splitting, quick and dirty because
// these advent projects have to be fast
parts := strings.Split(text, ":")
// Parse out game header
game, rest := parts[0], parts[1]
id, _ := strconv.Atoi(strings.Split(game, " ")[1])
zap.L().Sugar().Debugw("Got game Id",
"Id", id,
)
sRounds := strings.Split(rest, ";")
var rounds []Round
for _, sRound := range sRounds {
round := NewRoundFromString(sRound)
zap.L().Sugar().Debugw("Got round",
"round", round,
)
rounds = append(rounds, round)
}
return Game{
Id: id,
Rounds: rounds,
}
}
func parseGamesFile(file io.Reader) ([]Game, error) {
var games []Game
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if err := scanner.Err(); err != nil {
return []Game{}, err
} else {
theGame := NewGameFromString(scanner.Text())
zap.L().Sugar().Debugw("Got game",
"game", theGame,
)
games = append(games, theGame)
}
}
return games, nil
}
func loadGames(inputFileName string) ([]Game, error) {
file, err := os.Open(fmt.Sprintf("input/%s", inputFileName))
if err != nil {
return nil, err
}
defer func(file *os.File) {
err := file.Close()
if err != nil {
zap.L().Sugar().Error("err", err)
}
}(file)
return parseGamesFile(file)
}
func main() {
logger, _ := zap.NewProduction()
zap.ReplaceGlobals(logger)
defer zap.L().Sync() // flush buffer on close
//filename := os.Args[1]
filename := "input.txt"
zap.L().Sugar().Infow("Parsing file", "filename", filename)
// Load games
games, err := loadGames(filename)
if err != nil {
panic(err)
}
zap.L().Sugar().Infow("Loaded games",
"count", len(games),
)
// 12 red cubes, 13 green cubes, and 14 blue cubes
gameEvaluator := GameEvaluator{
Balls: []Ball{
{Count: 12,
Color: "red",
}, {
Count: 13,
Color: "green",
}, {
Count: 14,
Color: "blue",
},
},
}
var validGameIdSum, validGameCount int
for _, game := range games {
if gameEvaluator.Evaluate(game) {
validGameIdSum += game.Id
validGameCount += 1
}
}
logger.Sugar().Infow("summed valid game ids",
"validGameIdSum", validGameIdSum,
"validGameCount", validGameCount,
)
var minimumBallPowerSum int
for _, game := range games {
var minimizer GameMinimizer
minimizer.Minimize(game)
var powAccumulator int
for _, color := range minimizer.Colors() {
if powAccumulator == 0 {
powAccumulator = minimizer.GetBallCount(color)
} else {
powAccumulator *= minimizer.GetBallCount(color)
}
}
minimumBallPowerSum += powAccumulator
}
zap.L().Sugar().Infow("Minimized games",
"minimizedGameSum", minimumBallPowerSum,
)
}