From 05dd29a6835f98458dc291597e7c049af0a40a96 Mon Sep 17 00:00:00 2001 From: "Valeriano A.R" Date: Wed, 12 Dec 2018 21:06:15 +0100 Subject: [PATCH] Day09 --- .../AdventOfCode2018.Tests.csproj | 2 + AdventOfCode2018.Tests/Day09_Tests.cs | 68 +++++++ AdventOfCode2018.Tests/MarbleGame_Tests.cs | 74 ++++++++ AdventOfCode2018/AdventOfCode2018.csproj | 4 + AdventOfCode2018/Day09.cs | 174 ++++++++++++++++++ AdventOfCode2018/Program.cs | 3 +- AdventOfCode2018/inputs/Day09.txt | 1 + 7 files changed, 325 insertions(+), 1 deletion(-) create mode 100644 AdventOfCode2018.Tests/Day09_Tests.cs create mode 100644 AdventOfCode2018.Tests/MarbleGame_Tests.cs create mode 100644 AdventOfCode2018/Day09.cs create mode 100644 AdventOfCode2018/inputs/Day09.txt diff --git a/AdventOfCode2018.Tests/AdventOfCode2018.Tests.csproj b/AdventOfCode2018.Tests/AdventOfCode2018.Tests.csproj index 1693576..d22115a 100644 --- a/AdventOfCode2018.Tests/AdventOfCode2018.Tests.csproj +++ b/AdventOfCode2018.Tests/AdventOfCode2018.Tests.csproj @@ -67,7 +67,9 @@ + + diff --git a/AdventOfCode2018.Tests/Day09_Tests.cs b/AdventOfCode2018.Tests/Day09_Tests.cs new file mode 100644 index 0000000..98a086b --- /dev/null +++ b/AdventOfCode2018.Tests/Day09_Tests.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/AdventOfCode2018.Tests/MarbleGame_Tests.cs b/AdventOfCode2018.Tests/MarbleGame_Tests.cs new file mode 100644 index 0000000..4a5748a --- /dev/null +++ b/AdventOfCode2018.Tests/MarbleGame_Tests.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/AdventOfCode2018/AdventOfCode2018.csproj b/AdventOfCode2018/AdventOfCode2018.csproj index c9d013b..1f4b8f7 100644 --- a/AdventOfCode2018/AdventOfCode2018.csproj +++ b/AdventOfCode2018/AdventOfCode2018.csproj @@ -51,6 +51,7 @@ + @@ -80,6 +81,9 @@ PreserveNewest + + PreserveNewest + \ No newline at end of file diff --git a/AdventOfCode2018/Day09.cs b/AdventOfCode2018/Day09.cs new file mode 100644 index 0000000..8f34532 --- /dev/null +++ b/AdventOfCode2018/Day09.cs @@ -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 Scores { get; } = new Dictionary(); + + 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(); + } + } +} diff --git a/AdventOfCode2018/Program.cs b/AdventOfCode2018/Program.cs index 3761abf..1687f04 100644 --- a/AdventOfCode2018/Program.cs +++ b/AdventOfCode2018/Program.cs @@ -7,7 +7,7 @@ namespace AdventOfCode2018 { private static void Main(string[] args) { - int currentDayNumber = 8; + int currentDayNumber = 9; IDay currentDay = null; switch (currentDayNumber) @@ -20,6 +20,7 @@ namespace AdventOfCode2018 case 6: currentDay = new Day06(); break; case 7: currentDay = new Day07(); 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)); diff --git a/AdventOfCode2018/inputs/Day09.txt b/AdventOfCode2018/inputs/Day09.txt new file mode 100644 index 0000000..5557a77 --- /dev/null +++ b/AdventOfCode2018/inputs/Day09.txt @@ -0,0 +1 @@ +441 players; last marble is worth 71032 points