diff --git a/AdventOfCode/AdventOfCode.csproj b/AdventOfCode/AdventOfCode.csproj index 344a343..c282ccf 100644 --- a/AdventOfCode/AdventOfCode.csproj +++ b/AdventOfCode/AdventOfCode.csproj @@ -47,6 +47,7 @@ + diff --git a/AdventOfCode/Problems/AOC2023/Day4/Scratchcards.cs b/AdventOfCode/Problems/AOC2023/Day4/Scratchcards.cs index e410107..b7d380f 100644 --- a/AdventOfCode/Problems/AOC2023/Day4/Scratchcards.cs +++ b/AdventOfCode/Problems/AOC2023/Day4/Scratchcards.cs @@ -11,6 +11,7 @@ namespace AdventOfCode.Problems.AOC2023.Day4; internal class Scratchcards : Problem { private (int card, int[] win, int[] have)[] _cards = []; + private Dictionary _cardCount = new (); public override void CalculatePart1() { @@ -22,7 +23,32 @@ internal class Scratchcards : Problem public override void CalculatePart2() { - throw new NotImplementedException(); + Part2 = _cards.Length; + for (int i = 0; i < _cards.Length; i++) + { + var card = _cards[i]; + var wins = card.have.Intersect(card.win).Count(); + for (int j = 1; j <= wins; j++) + AddCards(card.card + j, GetCardCount(card.card)); + Part2 += wins * GetCardCount(card.card); + //_cardCount[card.card] = 0; + } + } + + private int GetCardCount(int card) + { + if(_cardCount.TryGetValue(card, out var count)) + return count; + //_cardCount.Add(card, 1); + return 1; + } + + private void AddCards(int card, int count) + { + if(_cardCount.ContainsKey(card)) + _cardCount[card] += count; + else + _cardCount.Add(card, count + 1); } public override void LoadInput() diff --git a/AdventOfCode/Problems/AOC2023/Day4/sample.txt b/AdventOfCode/Problems/AOC2023/Day4/sample.txt new file mode 100644 index 0000000..71f208a --- /dev/null +++ b/AdventOfCode/Problems/AOC2023/Day4/sample.txt @@ -0,0 +1,6 @@ +Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53 +Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19 +Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1 +Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83 +Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36 +Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11 \ No newline at end of file