adventofcode/2018/internal/day09/game.go
2018-12-11 19:11:45 -05:00

40 lines
797 B
Go

package day09
// Returns the winning player's index and score
func PlayGame(numPlayers, finNum int) (int, int) {
players := make([]int, numPlayers)
// initialize the 'board'
currentMarble := &Marble{Number: 0}
currentMarble.CW = currentMarble
currentMarble.CCW = currentMarble
currentPlayer := 0
for i := 1; currentMarble.Number < finNum; i++ {
var score int
currentMarble, score = currentMarble.PlaceNewMarble(i)
players[currentPlayer] += score
currentPlayer++
if currentPlayer >= len(players) {
currentPlayer = 0
}
}
return findWinnerData(players)
}
func findWinnerData(players []int) (int, int) {
topScore := -1
winner := -1
for player, score := range players {
if score > topScore {
topScore = score
winner = player
}
}
return winner, topScore
}