r/adventofcode • • Dec 09 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 9 Solutions -🎄-

--- Day 9: Marble Mania ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 9

Transcript:

Studies show that AoC programmers write better code after being exposed to ___.


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked at 00:29:13!

22 Upvotes

283 comments sorted by

View all comments

Show parent comment

1

u/d-sky Dec 09 '18

If you need only 1 element/ring you do not need to use ring.New(). You can directly use a Ring literal.

For example: &ring.Ring{Value: i})

Instead of:

s := ring.New(1)
s.Value = i

1

u/d-sky Dec 09 '18

My solution:

package main

import (
	"bufio"
	"container/ring"
	"fmt"
	"os"
)

func main() {
	fd, err := os.Open("input.txt")
	if err != nil {
		panic(err)
	}
	s := bufio.NewScanner(fd)

	for s.Scan() {
		var nPlayers, lastMarble int
		_, err := fmt.Sscanf(s.Text(), "%d players; last marble is worth %d points", &nPlayers, &lastMarble)
		if err != nil {
			panic(err)
		}

		players := make([]int, nPlayers)
		r := &ring.Ring{Value: 0}
		for i := 1; i <= lastMarble; i++ {
			if i%23 == 0 {
				r = r.Move(-8)
				players[(i-1)%nPlayers] += i + r.Unlink(1).Value.(int)
				r = r.Next()
			} else {
				r = r.Next().Link(&ring.Ring{Value: i}).Prev()
			}
		}
		var highScore int
		for _, s := range players {
			if s > highScore {
				highScore = s
			}
		}
		fmt.Println(highScore)
	}
}

1

u/ThezeeZ Dec 09 '18

Sleepy me wasn't sure if that would be initialized correctly. Looking at the source now I see it has it's own init, learned that that was possible today, neat! Edit: Scratch that, they're just calling it themselves.

If I remember correctly last year also had a task that looked like it was made for ring, but performance was abysmal, at least for my implementation.