Created
December 9, 2018 10:03
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"container/ring" | |
"flag" | |
"fmt" | |
"os" | |
) | |
type Input struct { | |
Line string | |
Players int | |
LastMarblePoints int | |
} | |
func readInput(filename string) []Input { | |
fileHandle, _ := os.Open(filename) | |
defer fileHandle.Close() | |
fileScanner := bufio.NewScanner(fileHandle) | |
inputList := []Input{} | |
for fileScanner.Scan() { | |
line := fileScanner.Text() | |
if len(line) > 0 { | |
i := Input{} | |
i.Line = line | |
fmt.Sscanf(line, "%d players; last marble is worth %d points", &i.Players, &i.LastMarblePoints) | |
inputList = append(inputList, i) | |
} | |
} | |
return inputList | |
} | |
var file = flag.String("file", "./input.txt", "file used for input") | |
func main() { | |
flag.Parse() | |
inputList := readInput(*file) | |
play(inputList, 1) | |
play(inputList, 100) | |
} | |
func play(inputList []Input, multiply int) { | |
for _, input := range inputList { | |
input.LastMarblePoints = input.LastMarblePoints * multiply | |
r := ring.New(1) | |
r.Value = 0 | |
scores := make(map[int]int, input.Players) | |
curPlayer := 1 | |
for x := 1; x < input.LastMarblePoints; x++ { | |
if x%23 == 0 { | |
r = r.Move(-8) | |
unl := r.Unlink(1) | |
scores[curPlayer] += unl.Value.(int) + x | |
r = r.Next() | |
} else { | |
nr := ring.New(1) | |
nr.Value = x | |
r = r.Move(1) | |
r = r.Link(nr) | |
r = r.Prev() | |
} | |
curPlayer ++ | |
if curPlayer > input.Players { | |
curPlayer = 1 | |
} | |
} | |
highest := 0 | |
highestScoringPlayer := 0 | |
for pl, score := range scores { | |
if score > highest { | |
highest = score | |
highestScoringPlayer = pl | |
} | |
} | |
fmt.Printf("Last marble %d, curPlayer %d, high score %d\n", input.LastMarblePoints, highestScoringPlayer, highest) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment