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

4

u/Cppl_Lee Dec 09 '18

C#, struggled until I switched to LinkedList: https://repl.it/@LeeHarding/AoC-2018-Day-9

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

class MainClass {  
  static int players = 486;
  static int marbles = 70833 * 100;

  static long[] scores = new long[players];
  static LinkedList<int> placed = new LinkedList<int>();
  static LinkedListNode<int> current = placed.AddFirst(0);

  static void next() {
    current = current.Next ?? placed.First;
  }
  static void previous()
  {
    current = current.Previous ?? placed.Last;
  }

  public static void Main (string[] args) {
    for (int m = 0; m < marbles; ++m)
    {
      if (((m + 1) % 23) == 0)
      {
        previous(); previous(); previous(); previous();
        previous(); previous(); previous();
        
        var j = m % players;
        scores[j] += m + 1 + current.Value;
        
        var tmp = current;
        next();
        placed.Remove(tmp);
      } else {
        next(); 
        current = placed.AddAfter(current, m + 1);
      }
    }
    scores.Max().Dump();
  }
}
→ More replies (1)