2021-12-21 22:21:30 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-12-21 23:22:04 +00:00
|
|
|
"AdventOfCode2021/shared"
|
2021-12-21 22:21:30 +00:00
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
content := returnContent("../input")
|
|
|
|
// content := returnContent("../testInput")
|
|
|
|
|
|
|
|
//Construct Game board... in reverse :) (Just makes it easier for stringing them together)
|
2021-12-21 23:22:04 +00:00
|
|
|
positions := make(map[int]*shared.GamePosition)
|
|
|
|
gameBoard := shared.GameBoard{
|
|
|
|
Start: &shared.GamePosition{
|
2021-12-21 22:21:30 +00:00
|
|
|
Position: 1,
|
|
|
|
Next: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
positions[1] = gameBoard.Start
|
|
|
|
next := gameBoard.Start
|
2021-12-21 23:22:04 +00:00
|
|
|
var currentPosition *shared.GamePosition
|
2021-12-21 22:21:30 +00:00
|
|
|
|
|
|
|
for i := 10; i > 1; i-- {
|
2021-12-21 23:22:04 +00:00
|
|
|
currentPosition = new(shared.GamePosition)
|
2021-12-21 22:21:30 +00:00
|
|
|
currentPosition.Position = i
|
|
|
|
currentPosition.Next = next
|
|
|
|
next = currentPosition
|
|
|
|
|
|
|
|
positions[i] = currentPosition
|
|
|
|
}
|
|
|
|
gameBoard.Start.Next = currentPosition
|
|
|
|
|
|
|
|
//Add Players to the game
|
2021-12-21 23:22:04 +00:00
|
|
|
players := make(map[int]*shared.Player)
|
2021-12-21 22:21:30 +00:00
|
|
|
numPlayers := 0
|
|
|
|
|
|
|
|
for _, value := range *content {
|
2021-12-21 23:22:04 +00:00
|
|
|
newPlayer := shared.Player{
|
2021-12-21 22:21:30 +00:00
|
|
|
PlayerNum: value[0],
|
|
|
|
Score: 0,
|
|
|
|
Position: positions[value[1]],
|
|
|
|
}
|
|
|
|
players[newPlayer.PlayerNum] = &newPlayer
|
|
|
|
numPlayers++
|
|
|
|
}
|
|
|
|
|
|
|
|
//Play the GAMEEEEEE
|
|
|
|
diceVal := 1
|
|
|
|
numRolls := 0
|
|
|
|
winner := 0
|
|
|
|
|
|
|
|
for {
|
|
|
|
for i := 1; i <= numPlayers; i++ {
|
|
|
|
p := players[i]
|
2021-12-21 23:22:04 +00:00
|
|
|
if Roll(p, &diceVal, &numRolls) {
|
2021-12-21 22:21:30 +00:00
|
|
|
winner = p.PlayerNum
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if winner > 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
loser := 1
|
|
|
|
if winner == 1 {
|
|
|
|
loser = 2
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(numRolls * players[loser].Score)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-12-21 23:22:04 +00:00
|
|
|
func Roll(p *shared.Player, diceVal *int, numRolls *int) bool {
|
2021-12-21 22:21:30 +00:00
|
|
|
//simulate 3 consecutive dice rolls
|
|
|
|
dice := (*diceVal + 1) * 3
|
|
|
|
*diceVal += 3
|
|
|
|
*numRolls += 3
|
|
|
|
|
|
|
|
//calculate where the position will be after n positions being moved
|
|
|
|
position := p.Position
|
|
|
|
for i := 0; i < dice; i++ {
|
|
|
|
position = position.Next
|
|
|
|
}
|
|
|
|
|
|
|
|
p.Position = position
|
|
|
|
|
|
|
|
p.Score += p.Position.Position
|
|
|
|
|
|
|
|
//Return bool for if player has won
|
|
|
|
return p.Score >= 1000
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func returnContent(path string) *[][]int {
|
|
|
|
//read file and return it as an array of integers
|
|
|
|
|
|
|
|
file, err := os.Open(path)
|
|
|
|
var content [][]int
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Unlucky, the file didn't open")
|
|
|
|
return &content
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
|
|
|
|
regex, _ := regexp.Compile(`[0-9]+`)
|
|
|
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
|
|
|
|
line := regex.FindAllString(scanner.Text(), 2)
|
|
|
|
nums := []int{}
|
|
|
|
|
|
|
|
for i := 0; i < len(line); i++ {
|
|
|
|
num, _ := strconv.Atoi(line[i])
|
|
|
|
nums = append(nums, num)
|
|
|
|
}
|
|
|
|
content = append(content, nums)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &content
|
|
|
|
}
|