-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1ffba41
commit 24ebb9f
Showing
7 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
428 players; last marble is worth 72061 points |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace AoC2018.Solutions.Day09 | ||
{ | ||
using System.Diagnostics; | ||
|
||
[DebuggerDisplay("{Number} : {CounterClockwise.Number} - {Number} - {Clockwise.Number}")] | ||
public class Marble | ||
{ | ||
public int Number { get; set; } | ||
|
||
public Marble Clockwise { get; set; } | ||
|
||
public Marble CounterClockwise { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
namespace AoC2018.Solutions.Day09 | ||
{ | ||
using System.Text; | ||
|
||
public static class MarbleExtensions | ||
{ | ||
public static Marble Clockwise(this Marble marble, int count) | ||
{ | ||
Marble current = marble; | ||
for (int i = 0; i < count; i++) | ||
{ | ||
current = current.Clockwise; | ||
} | ||
|
||
return current; | ||
} | ||
|
||
public static Marble CounterClockwise(this Marble marble, int count) | ||
{ | ||
Marble current = marble; | ||
for (int i = 0; i < count; i++) | ||
{ | ||
current = current.CounterClockwise; | ||
} | ||
|
||
return current; | ||
} | ||
|
||
public static Marble InsertAfter(this Marble marble, int number) | ||
{ | ||
Marble next = marble.Clockwise; | ||
var newMarble = new Marble | ||
{ | ||
Number = number, | ||
Clockwise = next, | ||
CounterClockwise = marble, | ||
}; | ||
|
||
marble.Clockwise = newMarble; | ||
next.CounterClockwise = newMarble; | ||
|
||
return newMarble; | ||
} | ||
|
||
public static Marble Remove(this Marble marble) | ||
{ | ||
Marble prev = marble.CounterClockwise; | ||
Marble next = marble.Clockwise; | ||
|
||
prev.Clockwise = next; | ||
next.CounterClockwise = prev; | ||
|
||
marble.Clockwise = null; | ||
marble.CounterClockwise = null; | ||
|
||
return marble; | ||
} | ||
|
||
public static string Visualise(this Marble start, Marble current) | ||
{ | ||
var visualisation = new StringBuilder(); | ||
|
||
Marble active = start; | ||
|
||
do | ||
{ | ||
if (active == current) | ||
{ | ||
visualisation.Append("("); | ||
visualisation.Append(active.Number); | ||
visualisation.Append(")"); | ||
} | ||
else | ||
{ | ||
visualisation.Append(active.Number); | ||
} | ||
|
||
visualisation.Append(" "); | ||
active = active.Clockwise; | ||
} | ||
while (active != start); | ||
|
||
return visualisation.ToString(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
namespace AoC2018.Solutions.Day09 | ||
{ | ||
using System; | ||
using System.Linq; | ||
|
||
public class Part01 : ISolution | ||
{ | ||
public string Solve(string input) | ||
{ | ||
string[] components = input.Split(' '); | ||
|
||
return this.Solve(int.Parse(components[0]), int.Parse(components[6])).ToString(); | ||
} | ||
|
||
public long Solve(int players, int marbles) | ||
{ | ||
long[] scores = new long[players]; | ||
|
||
// Set up Marble 0. | ||
// We're going to solve this using a doubly linked list | ||
var marble0 = new Marble | ||
{ | ||
Number = 0, | ||
}; | ||
|
||
marble0.Clockwise = marble0; | ||
marble0.CounterClockwise = marble0; | ||
|
||
Marble currentMarble = marble0; | ||
int currentPlayer = 0; | ||
|
||
for (int i = 1; i <= marbles; i++) | ||
{ | ||
if (i % 23 == 0) | ||
{ | ||
// Current player keeps this marble | ||
scores[currentPlayer] += i; | ||
|
||
// Also keeps the marble 7 counter clockwise | ||
Marble marbleToRemove = currentMarble.CounterClockwise(7); | ||
currentMarble = marbleToRemove.Clockwise; | ||
scores[currentPlayer] += marbleToRemove.Number; | ||
marbleToRemove.Remove(); | ||
} | ||
else | ||
{ | ||
currentMarble = currentMarble.Clockwise(1).InsertAfter(i); | ||
} | ||
|
||
currentPlayer = ++currentPlayer % players; | ||
} | ||
|
||
return scores.Max(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
namespace AoC2018.Solutions.Day09 | ||
{ | ||
using System; | ||
using System.Linq; | ||
|
||
public class Part02 : ISolution | ||
{ | ||
public string Solve(string input) | ||
{ | ||
var part1 = new Part01(); | ||
|
||
string[] components = input.Split(' '); | ||
|
||
return part1.Solve(int.Parse(components[0]), int.Parse(components[6]) * 100).ToString(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters