-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day4.cs
54 lines (45 loc) · 1.55 KB
/
Day4.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
namespace AoC2023;
public class Day4 : IDay<IEnumerable<string>, int>
{
public static int SolvePart1(IEnumerable<string> input) =>
input
.Select(GetPoints)
.Where(points => points != 0)
.Select(points => (int)Math.Pow(2, points - 1))
.Sum();
public static int SolvePart2(IEnumerable<string> input)
{
Dictionary<int, int> cardIdToCount = [];
var lines = input.ToArray();
for (var i = 0; i < lines.Length; i++)
{
var line = lines[i];
var counter = GetPoints(line);
if (!cardIdToCount.ContainsKey(i + 1))
{
cardIdToCount[i + 1] = 1;
}
for (int j = i + 2; j < i + 2 + counter; j++)
{
if (cardIdToCount.TryGetValue(j, out int value))
{
cardIdToCount[j] = value + cardIdToCount[i + 1];
}
else
{
cardIdToCount[j] = 1 + cardIdToCount[i + 1];
}
}
}
return cardIdToCount.Values.Sum();
}
private static IEnumerable<int> ParseCards(string str) =>
str.Split(' ').Where(s => !string.IsNullOrWhiteSpace(s)).Select(int.Parse);
private static int GetPoints(string line)
{
var cards = line.Split(": ")[1].Split(" | ");
var winningCards = ParseCards(cards[0]).ToHashSet();
var myCards = ParseCards(cards[1]).ToHashSet();
return myCards.Intersect(winningCards).Count();
}
}