Day09
This commit is contained in:
@@ -67,7 +67,9 @@
|
|||||||
<Compile Include="Day06_Tests.cs" />
|
<Compile Include="Day06_Tests.cs" />
|
||||||
<Compile Include="Day07_Tests.cs" />
|
<Compile Include="Day07_Tests.cs" />
|
||||||
<Compile Include="Day08_Tests.cs" />
|
<Compile Include="Day08_Tests.cs" />
|
||||||
|
<Compile Include="Day09_Tests.cs" />
|
||||||
<Compile Include="GuardEvent_Tests.cs" />
|
<Compile Include="GuardEvent_Tests.cs" />
|
||||||
|
<Compile Include="MarbleGame_Tests.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
68
AdventOfCode2018.Tests/Day09_Tests.cs
Normal file
68
AdventOfCode2018.Tests/Day09_Tests.cs
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
|
|
||||||
|
namespace AdventOfCode2018.Tests
|
||||||
|
{
|
||||||
|
[TestClass()]
|
||||||
|
public class Day09_Tests
|
||||||
|
{
|
||||||
|
[TestMethod()]
|
||||||
|
public void ResolvePart1__Test1()
|
||||||
|
{
|
||||||
|
Day09 day = new Day09();
|
||||||
|
|
||||||
|
string result = day.ResolvePart1(new string[] { "9 players; last marble is worth 25 points" });
|
||||||
|
|
||||||
|
Assert.AreEqual("32", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod()]
|
||||||
|
public void ResolvePart1__Test2()
|
||||||
|
{
|
||||||
|
Day09 day = new Day09();
|
||||||
|
|
||||||
|
string result = day.ResolvePart1(new string[] { "10 players; last marble is worth 1618 points" });
|
||||||
|
|
||||||
|
Assert.AreEqual("8317", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod()]
|
||||||
|
public void ResolvePart1__Test3()
|
||||||
|
{
|
||||||
|
Day09 day = new Day09();
|
||||||
|
|
||||||
|
string result = day.ResolvePart1(new string[] { "13 players; last marble is worth 7999 points" });
|
||||||
|
|
||||||
|
Assert.AreEqual("146373", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod()]
|
||||||
|
public void ResolvePart1__Test4()
|
||||||
|
{
|
||||||
|
Day09 day = new Day09();
|
||||||
|
|
||||||
|
string result = day.ResolvePart1(new string[] { "17 players; last marble is worth 1104 points" });
|
||||||
|
|
||||||
|
Assert.AreEqual("2764", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod()]
|
||||||
|
public void ResolvePart1__Test5()
|
||||||
|
{
|
||||||
|
Day09 day = new Day09();
|
||||||
|
|
||||||
|
string result = day.ResolvePart1(new string[] { "21 players; last marble is worth 6111 points" });
|
||||||
|
|
||||||
|
Assert.AreEqual("54718", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod()]
|
||||||
|
public void ResolvePart1__Test6()
|
||||||
|
{
|
||||||
|
Day09 day = new Day09();
|
||||||
|
|
||||||
|
string result = day.ResolvePart1(new string[] { "30 players; last marble is worth 5807 points" });
|
||||||
|
|
||||||
|
Assert.AreEqual("37305", result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
74
AdventOfCode2018.Tests/MarbleGame_Tests.cs
Normal file
74
AdventOfCode2018.Tests/MarbleGame_Tests.cs
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
|
|
||||||
|
namespace AdventOfCode2018.Tests
|
||||||
|
{
|
||||||
|
[TestClass()]
|
||||||
|
public class MarbleGame_Tests
|
||||||
|
{
|
||||||
|
[TestMethod()]
|
||||||
|
public void PlayGame__Test1()
|
||||||
|
{
|
||||||
|
MarbleGame marbleGame = new MarbleGame();
|
||||||
|
|
||||||
|
marbleGame.PlayGame(9, 25);
|
||||||
|
long highScore = marbleGame.GetHighScore();
|
||||||
|
|
||||||
|
Assert.AreEqual(32, highScore);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod()]
|
||||||
|
public void PlayGame__Test2()
|
||||||
|
{
|
||||||
|
MarbleGame marbleGame = new MarbleGame();
|
||||||
|
|
||||||
|
marbleGame.PlayGame(10, 1618);
|
||||||
|
long highScore = marbleGame.GetHighScore();
|
||||||
|
|
||||||
|
Assert.AreEqual(8317, highScore);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod()]
|
||||||
|
public void PlayGame__Test3()
|
||||||
|
{
|
||||||
|
MarbleGame marbleGame = new MarbleGame();
|
||||||
|
|
||||||
|
marbleGame.PlayGame(13, 7999);
|
||||||
|
long highScore = marbleGame.GetHighScore();
|
||||||
|
|
||||||
|
Assert.AreEqual(146373, highScore);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod()]
|
||||||
|
public void PlayGame__Test4()
|
||||||
|
{
|
||||||
|
MarbleGame marbleGame = new MarbleGame();
|
||||||
|
|
||||||
|
marbleGame.PlayGame(17, 1104);
|
||||||
|
long highScore = marbleGame.GetHighScore();
|
||||||
|
|
||||||
|
Assert.AreEqual(2764, highScore);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod()]
|
||||||
|
public void PlayGame__Test5()
|
||||||
|
{
|
||||||
|
MarbleGame marbleGame = new MarbleGame();
|
||||||
|
|
||||||
|
marbleGame.PlayGame(21, 6111);
|
||||||
|
long highScore = marbleGame.GetHighScore();
|
||||||
|
|
||||||
|
Assert.AreEqual(54718, highScore);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod()]
|
||||||
|
public void PlayGame__Test6()
|
||||||
|
{
|
||||||
|
MarbleGame marbleGame = new MarbleGame();
|
||||||
|
|
||||||
|
marbleGame.PlayGame(30, 5807);
|
||||||
|
long highScore = marbleGame.GetHighScore();
|
||||||
|
|
||||||
|
Assert.AreEqual(37305, highScore);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -51,6 +51,7 @@
|
|||||||
<Compile Include="Day06.cs" />
|
<Compile Include="Day06.cs" />
|
||||||
<Compile Include="Day07.cs" />
|
<Compile Include="Day07.cs" />
|
||||||
<Compile Include="Day08.cs" />
|
<Compile Include="Day08.cs" />
|
||||||
|
<Compile Include="Day09.cs" />
|
||||||
<Compile Include="IDay.cs" />
|
<Compile Include="IDay.cs" />
|
||||||
<Compile Include="Program.cs" />
|
<Compile Include="Program.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
@@ -80,6 +81,9 @@
|
|||||||
<Content Include="inputs\Day08.txt">
|
<Content Include="inputs\Day08.txt">
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
</Content>
|
</Content>
|
||||||
|
<Content Include="inputs\Day09.txt">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</Content>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
</Project>
|
</Project>
|
||||||
174
AdventOfCode2018/Day09.cs
Normal file
174
AdventOfCode2018/Day09.cs
Normal file
@@ -0,0 +1,174 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace AdventOfCode2018
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
--- Day 9: Marble Mania ---
|
||||||
|
|
||||||
|
You talk to the Elves while you wait for your navigation system to initialize. To pass the time, they introduce you to their favorite marble game.
|
||||||
|
|
||||||
|
The Elves play this game by taking turns arranging the marbles in a circle according to very particular rules. The marbles are numbered starting with 0 and increasing by 1 until every marble has a number.
|
||||||
|
|
||||||
|
First, the marble numbered 0 is placed in the circle. At this point, while it contains only a single marble, it is still a circle: the marble is both clockwise from itself and counter-clockwise from itself. This marble is designated the current marble.
|
||||||
|
|
||||||
|
Then, each Elf takes a turn placing the lowest-numbered remaining marble into the circle between the marbles that are 1 and 2 marbles clockwise of the current marble. (When the circle is large enough, this means that there is one marble between the marble that was just placed and the current marble.) The marble that was just placed then becomes the current marble.
|
||||||
|
|
||||||
|
However, if the marble that is about to be placed has a number which is a multiple of 23, something entirely different happens. First, the current player keeps the marble they would have placed, adding it to their score. In addition, the marble 7 marbles counter-clockwise from the current marble is removed from the circle and also added to the current player's score. The marble located immediately clockwise of the marble that was removed becomes the new current marble.
|
||||||
|
|
||||||
|
For example, suppose there are 9 players. After the marble with value 0 is placed in the middle, each player (shown in square brackets) takes a turn. The result of each of those turns would produce circles of marbles like this, where clockwise is to the right and the resulting current marble is in parentheses:
|
||||||
|
|
||||||
|
[-] (0)
|
||||||
|
[1] 0 (1)
|
||||||
|
[2] 0 (2) 1
|
||||||
|
[3] 0 2 1 (3)
|
||||||
|
[4] 0 (4) 2 1 3
|
||||||
|
[5] 0 4 2 (5) 1 3
|
||||||
|
[6] 0 4 2 5 1 (6) 3
|
||||||
|
[7] 0 4 2 5 1 6 3 (7)
|
||||||
|
[8] 0 (8) 4 2 5 1 6 3 7
|
||||||
|
[9] 0 8 4 (9) 2 5 1 6 3 7
|
||||||
|
[1] 0 8 4 9 2(10) 5 1 6 3 7
|
||||||
|
[2] 0 8 4 9 2 10 5(11) 1 6 3 7
|
||||||
|
[3] 0 8 4 9 2 10 5 11 1(12) 6 3 7
|
||||||
|
[4] 0 8 4 9 2 10 5 11 1 12 6(13) 3 7
|
||||||
|
[5] 0 8 4 9 2 10 5 11 1 12 6 13 3(14) 7
|
||||||
|
[6] 0 8 4 9 2 10 5 11 1 12 6 13 3 14 7(15)
|
||||||
|
[7] 0(16) 8 4 9 2 10 5 11 1 12 6 13 3 14 7 15
|
||||||
|
[8] 0 16 8(17) 4 9 2 10 5 11 1 12 6 13 3 14 7 15
|
||||||
|
[9] 0 16 8 17 4(18) 9 2 10 5 11 1 12 6 13 3 14 7 15
|
||||||
|
[1] 0 16 8 17 4 18 9(19) 2 10 5 11 1 12 6 13 3 14 7 15
|
||||||
|
[2] 0 16 8 17 4 18 9 19 2(20)10 5 11 1 12 6 13 3 14 7 15
|
||||||
|
[3] 0 16 8 17 4 18 9 19 2 20 10(21) 5 11 1 12 6 13 3 14 7 15
|
||||||
|
[4] 0 16 8 17 4 18 9 19 2 20 10 21 5(22)11 1 12 6 13 3 14 7 15
|
||||||
|
[5] 0 16 8 17 4 18(19) 2 20 10 21 5 22 11 1 12 6 13 3 14 7 15
|
||||||
|
[6] 0 16 8 17 4 18 19 2(24)20 10 21 5 22 11 1 12 6 13 3 14 7 15
|
||||||
|
[7] 0 16 8 17 4 18 19 2 24 20(25)10 21 5 22 11 1 12 6 13 3 14 7 15
|
||||||
|
|
||||||
|
The goal is to be the player with the highest score after the last marble is used up. Assuming the example above ends after the marble numbered 25, the winning score is 23+9=32 (because player 5 kept marble 23 and removed marble 9, while no other player got any points in this very short example game).
|
||||||
|
|
||||||
|
Here are a few more examples:
|
||||||
|
|
||||||
|
10 players; last marble is worth 1618 points: high score is 8317
|
||||||
|
13 players; last marble is worth 7999 points: high score is 146373
|
||||||
|
17 players; last marble is worth 1104 points: high score is 2764
|
||||||
|
21 players; last marble is worth 6111 points: high score is 54718
|
||||||
|
30 players; last marble is worth 5807 points: high score is 37305
|
||||||
|
|
||||||
|
What is the winning Elf's score?
|
||||||
|
|
||||||
|
--- Part Two ---
|
||||||
|
|
||||||
|
Amused by the speed of your answer, the Elves are curious:
|
||||||
|
|
||||||
|
What would the new winning Elf's score be if the number of the last marble were 100 times larger?
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class Day09 : IDay
|
||||||
|
{
|
||||||
|
public string ResolvePart1(string[] inputs)
|
||||||
|
{
|
||||||
|
return CalculateHighScore(inputs[0], 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public string ResolvePart2(string[] inputs)
|
||||||
|
{
|
||||||
|
return CalculateHighScore(inputs[0], 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string CalculateHighScore(string input, long factor)
|
||||||
|
{
|
||||||
|
string[] parts = input.Split(new string[] { " players; last marble is worth ", " points" }, StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
long numberOfPlayers = Convert.ToInt32(parts[0]);
|
||||||
|
long lastMarble = Convert.ToInt32(parts[1]) * factor;
|
||||||
|
MarbleGame marbleGame = new MarbleGame();
|
||||||
|
marbleGame.PlayGame(numberOfPlayers, lastMarble);
|
||||||
|
long result = marbleGame.GetHighScore();
|
||||||
|
return result.ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Marble
|
||||||
|
{
|
||||||
|
public long Value { get; set; }
|
||||||
|
public Marble Previous { get; set; }
|
||||||
|
public Marble Next { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class MarbleGame
|
||||||
|
{
|
||||||
|
public Dictionary<long, long> Scores { get; } = new Dictionary<long, long>();
|
||||||
|
|
||||||
|
private Marble firstMarble;
|
||||||
|
private Marble currentMarble;
|
||||||
|
private long currentPlayer = 0;
|
||||||
|
|
||||||
|
private const long PointValueMultiple = 23;
|
||||||
|
|
||||||
|
public void PlayGame(long numPlayers, long lastMarble, bool showStatus = false)
|
||||||
|
{
|
||||||
|
Scores.Clear();
|
||||||
|
firstMarble = new Marble { Value = 0 };
|
||||||
|
firstMarble.Previous = firstMarble;
|
||||||
|
firstMarble.Next = firstMarble;
|
||||||
|
currentMarble = firstMarble;
|
||||||
|
|
||||||
|
for (long i = 1; i <= numPlayers; i++) { Scores.Add(i, 0); }
|
||||||
|
|
||||||
|
for (long i = 0; i <= lastMarble; i++)
|
||||||
|
{
|
||||||
|
if (showStatus) { PrintStatus(); }
|
||||||
|
|
||||||
|
currentPlayer = (i % numPlayers) + 1;
|
||||||
|
Marble newMarble = new Marble { Value = i + 1 };
|
||||||
|
if ((newMarble.Value % PointValueMultiple) > 0)
|
||||||
|
{
|
||||||
|
Marble previousMarble = currentMarble.Next;
|
||||||
|
Marble nextMarble = previousMarble.Next;
|
||||||
|
newMarble.Previous = previousMarble;
|
||||||
|
newMarble.Next = nextMarble;
|
||||||
|
previousMarble.Next = newMarble;
|
||||||
|
nextMarble.Previous = newMarble;
|
||||||
|
currentMarble = newMarble;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Marble marbleToRemove = currentMarble.Previous.Previous.Previous.Previous.Previous.Previous.Previous;
|
||||||
|
currentMarble = marbleToRemove.Next;
|
||||||
|
marbleToRemove.Previous.Next = marbleToRemove.Next;
|
||||||
|
marbleToRemove.Next.Previous = marbleToRemove.Previous;
|
||||||
|
|
||||||
|
long currentPlayerScore = Scores[currentPlayer] + (newMarble.Value + marbleToRemove.Value);
|
||||||
|
Scores[currentPlayer] = currentPlayerScore;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void PrintStatus()
|
||||||
|
{
|
||||||
|
Console.Write("[{0}] ", currentPlayer);
|
||||||
|
Marble marble = firstMarble;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
if (currentMarble.Value == marble.Value)
|
||||||
|
{
|
||||||
|
Console.Write("({0}) ", marble.Value);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Console.Write("{0} ", marble.Value);
|
||||||
|
}
|
||||||
|
marble = marble.Next;
|
||||||
|
} while (marble.Value != 0);
|
||||||
|
Console.WriteLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
public long GetHighScore()
|
||||||
|
{
|
||||||
|
return Scores.Values.Max();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,7 +7,7 @@ namespace AdventOfCode2018
|
|||||||
{
|
{
|
||||||
private static void Main(string[] args)
|
private static void Main(string[] args)
|
||||||
{
|
{
|
||||||
int currentDayNumber = 8;
|
int currentDayNumber = 9;
|
||||||
IDay currentDay = null;
|
IDay currentDay = null;
|
||||||
|
|
||||||
switch (currentDayNumber)
|
switch (currentDayNumber)
|
||||||
@@ -20,6 +20,7 @@ namespace AdventOfCode2018
|
|||||||
case 6: currentDay = new Day06(); break;
|
case 6: currentDay = new Day06(); break;
|
||||||
case 7: currentDay = new Day07(); break;
|
case 7: currentDay = new Day07(); break;
|
||||||
case 8: currentDay = new Day08(); break;
|
case 8: currentDay = new Day08(); break;
|
||||||
|
case 9: currentDay = new Day09(); break;
|
||||||
}
|
}
|
||||||
|
|
||||||
string[] linesDay = File.ReadAllLines(string.Format("inputs/Day{0:00}.txt", currentDayNumber));
|
string[] linesDay = File.ReadAllLines(string.Format("inputs/Day{0:00}.txt", currentDayNumber));
|
||||||
|
|||||||
1
AdventOfCode2018/inputs/Day09.txt
Normal file
1
AdventOfCode2018/inputs/Day09.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
441 players; last marble is worth 71032 points
|
||||||
Reference in New Issue
Block a user