Skip to content

Instantly share code, notes, and snippets.

@apeterson-BFI
Created December 9, 2018 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apeterson-BFI/002949c912a72bc7e3423e80d2f65b47 to your computer and use it in GitHub Desktop.
Save apeterson-BFI/002949c912a72bc7e3423e80d2f65b47 to your computer and use it in GitHub Desktop.
Advent of Code C# day 9
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AdventCSharp
{
public class CircularLinkedList
{
public CircularLinkListNode ActiveNode { get; set; }
public int NodeCount { get; set; }
public CircularLinkedList()
{
ActiveNode = new CircularLinkListNode() { Marble = 0L };
ActiveNode.Clockwise = ActiveNode;
ActiveNode.Counterclockwise = ActiveNode;
NodeCount = 1;
}
public CircularLinkedList(long marble)
{
ActiveNode = new CircularLinkListNode() { Marble = marble };
ActiveNode.Clockwise = ActiveNode;
ActiveNode.Counterclockwise = ActiveNode;
NodeCount = 1;
}
public void gameMarble(long marble)
{
var c1Node = ActiveNode.Clockwise;
var c2Node = c1Node.Clockwise;
var nNode = new CircularLinkListNode() { Marble = marble, Clockwise = c2Node, Counterclockwise = c1Node };
c1Node.Clockwise = nNode;
c2Node.Counterclockwise = nNode;
ActiveNode = nNode;
}
public long game23k(long marble)
{
long score = marble;
var remNode = ActiveNode.Counterclockwise.Counterclockwise.Counterclockwise.Counterclockwise.Counterclockwise.Counterclockwise.Counterclockwise;
score += remNode.Marble;
var rcc = remNode.Counterclockwise;
var rc = remNode.Clockwise;
rcc.Clockwise = rc;
rc.Counterclockwise = rcc;
ActiveNode = rc;
return score;
}
public static long playGame(int players, long lastMarble)
{
long[] playerScores = new long[players];
int activePlayer = 0;
CircularLinkedList list = new CircularLinkedList();
for (long marble = 1L; marble <= lastMarble; marble++)
{
if (marble % 23L == 0L)
{
playerScores[activePlayer] += list.game23k(marble);
}
else
{
list.gameMarble(marble);
}
activePlayer = ((activePlayer + 1) % players);
}
return playerScores.Max();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AdventCSharp
{
public class CircularLinkListNode
{
public CircularLinkListNode Counterclockwise { get; set; }
public CircularLinkListNode Clockwise { get; set; }
public long Marble { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment