Day 9.1 solution.
This commit is contained in:
parent
414202f524
commit
5c52fbdc16
4 changed files with 132 additions and 0 deletions
39
2018/internal/day09/game.go
Normal file
39
2018/internal/day09/game.go
Normal file
|
@ -0,0 +1,39 @@
|
|||
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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue