2018 Day09 Complete

This commit is contained in:
2018-12-03 18:11:38 +00:00
parent 2469a641db
commit 7dba9cd112
5 changed files with 384 additions and 0 deletions

143
2018/day09/day09.go Normal file
View File

@@ -0,0 +1,143 @@
package main
import (
"fmt"
"log"
"os"
"strconv"
)
var playerCount, marbleCount int
var game *Game
func main() {
if len(os.Args) < 3 {
fmt.Println("Usage: day09 <# players> <# marbles>")
os.Exit(1)
}
playerCount, marbleCount = Atoi(os.Args[1]), Atoi(os.Args[2])
part1()
}
func part1() {
g := NewGame(playerCount, marbleCount)
for i := 1; i <= marbleCount; i++ {
if i%23 == 0 {
g.score(i)
} else {
g.insertMarble(&Marble{value: i})
}
g.nextPlayer()
}
fmt.Println("* Winner *")
g.printWinner()
}
type Game struct {
playerCount, marbleCount int
currentPlayer int
scores map[int]int
currentMarble *Marble
}
func NewGame(players, marbles int) *Game {
startMarble := &Marble{
value: 0,
}
startMarble.clockwise = startMarble
startMarble.widdershins = startMarble
return &Game{
playerCount: players,
marbleCount: marbles,
currentPlayer: 1,
scores: make(map[int]int),
currentMarble: startMarble,
}
}
func (g *Game) insertMarble(m *Marble) {
m.clockwise = g.currentMarble.clockwise.clockwise
m.widdershins = g.currentMarble.clockwise
g.currentMarble.clockwise.clockwise.widdershins = m
g.currentMarble.clockwise.clockwise = m
g.currentMarble = m
}
func (g *Game) score(val int) {
g.scores[g.currentPlayer] += val
for i := 0; i < 6; i++ {
g.currentMarble = g.currentMarble.widdershins
}
// Remove g.currentMarble.widdershins
// adding it's value to the current player's score
rem := g.currentMarble.widdershins
g.currentMarble.widdershins = rem.widdershins
g.currentMarble.widdershins.clockwise = g.currentMarble
g.scores[g.currentPlayer] += rem.value
}
func (g *Game) nextPlayer() {
if g.currentPlayer == g.playerCount {
g.currentPlayer = 1
} else {
g.currentPlayer++
}
}
func (g *Game) printState() {
fmt.Printf("[%d] ", g.currentPlayer)
p := *g.currentMarble
for p.value != 0 {
// Find the '0' marble
p = *p.clockwise
}
if p.value == g.currentMarble.value {
fmt.Printf("(%d)", p.value)
} else {
fmt.Printf(" %d ", p.value)
}
p = *p.clockwise
for p.value != 0 {
if p.value == g.currentMarble.value {
fmt.Printf("(%d)", p.value)
} else {
fmt.Printf(" %d ", p.value)
}
p = *p.clockwise
}
fmt.Println("")
}
func (g *Game) printScores() {
for i := 1; i <= g.playerCount; i++ {
fmt.Println(i, ":", g.scores[i])
}
}
func (g *Game) printWinner() {
var top int
var winner int
for k, v := range g.scores {
if v > top {
top = v
winner = k
}
}
fmt.Println(winner, ":", top)
}
type Marble struct {
value int
clockwise *Marble
widdershins *Marble
}
func Atoi(i string) int {
var ret int
var err error
if ret, err = strconv.Atoi(i); err != nil {
log.Fatal("Invalid Atoi")
}
return ret
}

7
2018/day09/myinput.sh Executable file
View File

@@ -0,0 +1,7 @@
#!/bin/bash
echo "= Part 1 ="
./day09 448 71628
echo "= Part 2 ="
./day09 448 7162800

114
2018/day09/problem Normal file
View File

@@ -0,0 +1,114 @@
Advent of Code
--- Day 9: Marble Mania ---
You talk to the Elves while you wait for your navigation system to
initialize. To pass the time, they introduce you to their favorite marble
game.
The Elves play this game by taking turns arranging the marbles in a
circle according to very particular rules. The marbles are numbered
starting with 0 and increasing by 1 until every marble has a number.
First, the marble numbered 0 is placed in the circle. At this point,
while it contains only a single marble, it is still a circle: the marble
is both clockwise from itself and counter-clockwise from itself. This
marble is designated the current marble.
Then, each Elf takes a turn placing the lowest-numbered remaining marble
into the circle between the marbles that are 1 and 2 marbles clockwise of
the current marble. (When the circle is large enough, this means that
there is one marble between the marble that was just placed and the
current marble.) The marble that was just placed then becomes the current
marble.
However, if the marble that is about to be placed has a number which is a
multiple of 23, something entirely different happens. First, the current
player keeps the marble they would have placed, adding it to their score.
In addition, the marble 7 marbles counter-clockwise from the current
marble is removed from the circle and also added to the current player's
score. The marble located immediately clockwise of the marble that was
removed becomes the new current marble.
For example, suppose there are 9 players. After the marble with value 0
is placed in the middle, each player (shown in square brackets) takes a
turn. The result of each of those turns would produce circles of marbles
like this, where clockwise is to the right and the resulting current
marble is in parentheses:
[-] (0)
[1] 0 (1)
[2] 0 (2) 1
[3] 0 2 1 (3)
[4] 0 (4) 2 1 3
[5] 0 4 2 (5) 1 3
[6] 0 4 2 5 1 (6) 3
[7] 0 4 2 5 1 6 3 (7)
[8] 0 (8) 4 2 5 1 6 3 7
[9] 0 8 4 (9) 2 5 1 6 3 7
[1] 0 8 4 9 2(10) 5 1 6 3 7
[2] 0 8 4 9 2 10 5(11) 1 6 3 7
[3] 0 8 4 9 2 10 5 11 1(12) 6 3 7
[4] 0 8 4 9 2 10 5 11 1 12 6(13) 3 7
[5] 0 8 4 9 2 10 5 11 1 12 6 13 3(14) 7
[6] 0 8 4 9 2 10 5 11 1 12 6 13 3 14 7(15)
[7] 0(16) 8 4 9 2 10 5 11 1 12 6 13 3 14 7 15
[8] 0 16 8(17) 4 9 2 10 5 11 1 12 6 13 3 14 7 15
[9] 0 16 8 17 4(18) 9 2 10 5 11 1 12 6 13 3 14 7 15
[1] 0 16 8 17 4 18 9(19) 2 10 5 11 1 12 6 13 3 14 7 15
[2] 0 16 8 17 4 18 9 19 2(20)10 5 11 1 12 6 13 3 14 7 15
[3] 0 16 8 17 4 18 9 19 2 20 10(21) 5 11 1 12 6 13 3 14 7 15
[4] 0 16 8 17 4 18 9 19 2 20 10 21 5(22)11 1 12 6 13 3 14 7 15
[5] 0 16 8 17 4 18(19) 2 20 10 21 5 22 11 1 12 6 13 3 14 7 15
[6] 0 16 8 17 4 18 19 2(24)20 10 21 5 22 11 1 12 6 13 3 14 7 15
[7] 0 16 8 17 4 18 19 2 24 20(25)10 21 5 22 11 1 12 6 13 3 14 7 15
The goal is to be the player with the highest score after the last marble
is used up. Assuming the example above ends after the marble numbered 25,
the winning score is 23+9=32 (because player 5 kept marble 23 and removed
marble 9, while no other player got any points in this very short example
game).
Here are a few more examples:
* 10 players; last marble is worth 1618 points: high score is 8317
* 13 players; last marble is worth 7999 points: high score is 146373
* 17 players; last marble is worth 1104 points: high score is 2764
* 21 players; last marble is worth 6111 points: high score is 54718
* 30 players; last marble is worth 5807 points: high score is 37305
What is the winning Elf's score?
Your puzzle answer was 394486.
--- Part Two ---
Amused by the speed of your answer, the Elves are curious:
What would the new winning Elf's score be if the number of the last
marble were 100 times larger?
Your puzzle answer was 3276488008.
Both parts of this puzzle are complete! They provide two gold stars: **
References
Visible links
. https://adventofcode.com/
. https://adventofcode.com/2018/about
. https://adventofcode.com/2018/events
. https://adventofcode.com/2018/settings
. https://adventofcode.com/2018/auth/logout
. Advent of Code Supporter
https://adventofcode.com/2018/support
. https://adventofcode.com/2018
. https://adventofcode.com/2018
. https://adventofcode.com/2018/support
. https://adventofcode.com/2018/sponsors
. https://adventofcode.com/2018/leaderboard
. https://adventofcode.com/2018/stats
. https://adventofcode.com/2018/sponsors
. https://en.wikipedia.org/wiki/Marble_(toy)
. https://adventofcode.com/2018
. https://adventofcode.com/2018/day/9/input