Skip to content

Commit

Permalink
Part 2, works with example but fails live
Browse files Browse the repository at this point in the history
  • Loading branch information
tetsuo13 committed Jan 4, 2024
1 parent fcfb259 commit 81aff12
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
43 changes: 34 additions & 9 deletions AdventOfCode/Calendar/2023/Day04/Solution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,46 @@

internal class Solution : BaseSolution
{
private class Scratchcard
{
public IEnumerable<int> WinningNumbers { get; set; } = Enumerable.Empty<int>();
public IEnumerable<int> MyNumbers { get; set; } = Enumerable.Empty<int>();
public int Worth => (int)Math.Pow(2, WinningNumbers.Count() - 1);
}

public override async Task<int> Run(RunMode runMode)
{
return runMode switch
{
RunMode.PartOne => await TotalPoints(),
RunMode.PartTwo => await TotalScratchcards(),
_ => throw new ArgumentOutOfRangeException(nameof(runMode))
};
}

private class Scratchcard
private async Task<int> TotalPoints()
{
public IEnumerable<int> WinningNumbers { get; set; } = Enumerable.Empty<int>();
public IEnumerable<int> MyNumbers { get; set; } = Enumerable.Empty<int>();
public int Worth => (int)Math.Pow(2, WinningNumbers.Count() - 1);
var scratchcards = await CountCards();
return scratchcards.Sum(x => x.Worth);
}

private async Task<int> TotalPoints()
private async Task<int> TotalScratchcards()
{
var scratchcards = await CountCards();
var counts = Enumerable.Repeat(1, scratchcards.Count).ToArray();

for (var i = 0; i < scratchcards.Count; i++)
{
for (var j = 1; j <= scratchcards[i].WinningNumbers.Count(); j++)
{
counts[i + j] += counts[i];
}
}

return counts.Sum();
}

private async Task<List<Scratchcard>> CountCards()
{
var cards = await ReadInput();
var scratchcards = new List<Scratchcard>();
Expand All @@ -39,10 +62,12 @@ private async Task<int> TotalPoints()
scratchcards.Add(scratchcard);
}

return scratchcards.Sum(x => x.Worth);

return scratchcards;
}

private static IEnumerable<int> ParseOutNumbers(string s) =>
s.Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
private static IEnumerable<int> ParseOutNumbers(string s)
{
return s.Split(' ', StringSplitOptions.RemoveEmptyEntries)
.Select(int.Parse);
}
}
1 change: 1 addition & 0 deletions AdventOfCode/Calendar/2023/Day04/SolutionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ private class TestSolution(string[] inputLines) : Solution

[Theory]
[InlineData(RunMode.PartOne, 13)]
[InlineData(RunMode.PartTwo, 30)]
public async Task Example(RunMode runMode, int expected)
{
string[] input =
Expand Down

0 comments on commit 81aff12

Please sign in to comment.