Skip to content

Commit

Permalink
2018 Day14 Solved (little slow, but OK)
Browse files Browse the repository at this point in the history
  • Loading branch information
smabuk committed Nov 12, 2023
1 parent baea7d6 commit 1804445
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
1 change: 1 addition & 0 deletions Data/2018_14.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
846601
62 changes: 62 additions & 0 deletions Solutions/2018/Day14.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
namespace AdventOfCode.Solutions._2018;

/// <summary>
/// Day 14: Chocolate Charts
/// https://adventofcode.com/2018/day/14
/// </summary>
[Description("Chocolate Charts")]
public sealed partial class Day14 {

public static string Part1(string[] input, params object[]? _) => Solution1(input).ToString();
public static string Part2(string[] input, params object[]? _) => Solution2(input).ToString();

private static string Solution1(string[] input) {
int noOfRecipes = input[0].AsInt();

LinkedList<int> recipes = new([3, 7]);
string tail = "37";
List<LinkedListNode<int>> elfCurrentRecipes = [recipes.First!, recipes.Last!];

do {
_ = CreateNewRecipes(recipes, elfCurrentRecipes, tail);
} while (recipes.Count < noOfRecipes + 10);

return string.Join("", recipes.Skip(noOfRecipes).Take(10));
}

private static int Solution2(string[] input) {
string recipesToFind = input[0];

LinkedList<int> recipes = new([3, 7]);
string tail = "37";
List<LinkedListNode<int>> elfCurrentRecipes = [recipes.First!, recipes.Last!];

do {
tail = CreateNewRecipes(recipes, elfCurrentRecipes, tail);
} while (!tail.Contains(recipesToFind));

return string.Join("", recipes).IndexOf(recipesToFind);
}

private static string CreateNewRecipes(LinkedList<int> recipes, List<LinkedListNode<int>> elfCurrentRecipes, string tail)
{
string newRecipes = elfCurrentRecipes.Select(x => x.Value).Sum().ToString();
for (int i = 0; i < newRecipes.Length; i++) {
_ = recipes.AddLast(newRecipes[i].ToString().AsInt());
tail += newRecipes[i];
}

for (int i = 0; i < elfCurrentRecipes.Count; i++) {
LinkedListNode<int> recipe = elfCurrentRecipes[i];
int noOfSteps = recipe.Value + 1;
for (int steps = 0; steps < noOfSteps; steps++) {
recipe = recipe.Next ?? recipes.First!;
}
elfCurrentRecipes[i] = recipe;
}

return tail.Length > 9 ? tail[^9..] : tail;
}


}
29 changes: 29 additions & 0 deletions Tests/2018/Tests_14.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace AdventOfCode.Tests.Year2018;

public class Tests_14_Chocolate_Charts
{
const int DAY = 14;

[Theory]
[InlineData("9", "5158916779")]
[InlineData("5", "0124515891")]
[InlineData("18", "9251071085")]
[InlineData("2018", "5941429882")]
public void Part1(string input, string expected)
{
string actual = SolutionRouter.SolveProblem(YEAR, DAY, PART1, input);
Assert.Equal(expected, actual);
}

[Theory]
[InlineData("51589", 9 )]
[InlineData("01245", 5 )]
[InlineData("92510", 18)]
[InlineData("59414", 2018)]
public void Part21(string input, int expected)
{
_ = int.TryParse(SolutionRouter.SolveProblem(YEAR, DAY, PART2, input), out int actual);
Assert.Equal(expected, actual);
}

}

0 comments on commit 1804445

Please sign in to comment.