-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
54 lines (47 loc) · 1.66 KB
/
Program.cs
File metadata and controls
54 lines (47 loc) · 1.66 KB
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
internal class Program
{
private const string Path = @"..\..\..\input.txt";
private static void Main(string[] args)
{
Part1();
Part2();
}
private static void Part1()
{
int sum = 0;
foreach (string card in File.ReadLines(Path))
{
int matches = GetMatches(card);
if (matches > 0)
{
sum += 1 << (matches - 1);
}
}
Console.WriteLine($"Part 1: {sum}");
}
private static void Part2()
{
string[] cards = File.ReadAllLines(Path);
int[] cardAmounts = Enumerable.Repeat(1, cards.Length).ToArray();
for (int i = 0; i < cardAmounts.Length; i++)
{
int matches = GetMatches(cards[i]);
for (int j = i + 1; j <= i + matches && j < cardAmounts.Length; j++)
{
cardAmounts[j] += cardAmounts[i];
}
}
Console.WriteLine($"Part 2: {cardAmounts.Sum()}");
}
private static int GetMatches(string card)
{
if (card.Split(':') is not ([_, string numbersStr])
|| numbersStr.Split('|') is not ([string winningNumbersStr, string haveNumbersStr]))
{
throw new Exception($"Error parsing input '{card}'");
}
int[] winningNumbers = winningNumbersStr.Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
int[] haveNumbers = haveNumbersStr.Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
return haveNumbers.Count(number => winningNumbers.Contains(number));
}
}