-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday9.cs
51 lines (47 loc) · 1.69 KB
/
day9.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// https://adventofcode.com/2018/day/9
class Data9
{
LinkedListNode<int> Selected;
LinkedList<int> Circle;
public int Insert(int marble)
{
if (marble == 0)
{
if (Circle == null)
{
Circle = new LinkedList<int>();
}
Selected = Circle.AddLast(0);
}
else if (marble % 23 != 0)
{
LinkedListNode<int> next = (Selected.Next != null) ? Selected.Next : Circle.First;
Selected = Circle.AddAfter(next, marble);//.Insert(Selected, marble);
}
else // special case
{
for (int i = 0; i < 7; ++i)
{
Selected = (Selected.Previous != null) ? Selected.Previous : Circle.Last;
}
LinkedListNode<int> old = Selected;
int score = marble + old.Value;
Selected = (Selected.Next != null) ? Selected.Next : Circle.First;
Circle.Remove(old);
return score;
}
return 0;
}
}
static long Day9(int players, int marbles)
{
Data9 data = new Data9();
long[] scoreList = new long[players];
for (int m = 0; m <= marbles; ++m)
{
int score = data.Insert(m);
int playerIndex = (m % players);
scoreList[playerIndex] += score;
}
return scoreList.Max();
}