-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay4.cs
112 lines (88 loc) · 2.57 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace AOC23
{
public class Card
{
public int id;
public List<int> have = [];
public List<int> winning = [];
public int count;
public Card(string input)
{
var card_id_re = new Regex(@"Card\s+(\d+)");
this.id = int.Parse(card_id_re.Match(input.Split(':')[0]).Groups[1].Value);
var rem = input.Split(':')[1].Split('|');
string have = rem[1].Trim();
string win = rem[0].Trim();
foreach(string num in have.Split(' '))
{
if (num.Trim().Length > 0)
this.have.Add(int.Parse(num));
}
foreach(string num in win.Split(' '))
{
if (num.Trim().Length > 0)
this.winning.Add(int.Parse(num));
}
this.count = 1;
}
public int Matches
{
get
{
int count = 0;
foreach (var num in have)
{
if (winning.Contains(num))
{ count++; }
}
if (count == 0) { return 0; }
return count;
}
}
}
public class Day4(bool test) : Day(4, test)
{
public override void Part1()
{
Console.Write("\tPart 1: ");
var lines = this.Load();
var cards = new List<Card>();
int score = 0;
foreach (var line in lines)
{
var c = new Card(line);
score += (int)Math.Pow(2, c.Matches - 1);
cards.Add(c);
}
Console.WriteLine(score);
}
public override void Part2()
{
Console.Write("\tPart 2: ");
var lines = this.Load();
var cards = new List<Card>();
foreach (var line in lines)
{
var c = new Card(line);
cards.Add(c);
}
for (int i = 0; i < cards.Count; i++)
{
for (int j = i + 1; j <= cards[i].Matches + i; j++)
{
cards[j].count += cards[i].count;
}
}
int sum = 0;
foreach(Card c in cards)
{
sum += c.count;
}
Console.WriteLine(sum);
}
}
}