-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay4.cs
80 lines (68 loc) · 1.94 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System.Globalization;
namespace _2023_cs;
public class Day4
{
private static int solve_card(string line)
{
int winnings = 0;
HashSet<int> winning = new HashSet<int>();
int index = line.IndexOf(':')+2;
while (line[index] != '|')
{
int number = 0;
while (char.IsDigit(line[index]))
{
number *= 10;
number += line[index];
index++;
}
if (number != 0)
{
winning.Add(number);
}
index++;
}
while (index < line.Length)
{
int number = 0;
while (index < line.Length && char.IsDigit(line[index]) )
{
number *= 10;
number += line[index];
index++;
}
if (number != 0 && winning.Contains(number))
{
winnings += 1;
}
index++;
}
return winnings;
}
public static void solve(string filename)
{
int sum = 0,sum2 = 0;
StreamReader input = new StreamReader(filename);
string? line = input.ReadLine();
int[] copies = new int[202];
int cardIndex = 0;
while (line != null)
{
copies[cardIndex] += 1;
int points = solve_card(line);
if (points !=0)
{
sum += (int) Math.Pow(2,points-1);
for (int i = cardIndex+1; i < cardIndex+1+points; i++)
{
copies[i] += copies[cardIndex];
}
}
cardIndex++;
line = input.ReadLine();
}
sum2 += copies.Sum();
Console.WriteLine(sum);
Console.WriteLine(sum2);
}
}