[2023] Update Day4

Signed-off-by: TiagoRG <tiago.rgarcia@ua.pt>
This commit is contained in:
Tiago Garcia 2023-12-05 17:22:28 +00:00
parent d999774ec1
commit 343f1f08ba
Signed by: TiagoRG
GPG Key ID: DFCD48E3F420DB42
1 changed files with 4 additions and 10 deletions

View File

@ -7,8 +7,6 @@ public static class Day4
private class Card
{
public int CardNumber { get; set; }
public int[]? WinningNumbers { get; set; }
public int[]? Numbers { get; set; }
public int ValidNumbers { get; set; }
}
@ -22,15 +20,11 @@ public static class Day4
Card card = new Card();
card.CardNumber = Convert.ToInt32(Regex.Split(line.Split(": ")[0], @"\s+").Last());
string[] cardNumbers = Regex.Split(line.Split(": ")[1], @"\s+\|\s+");
card.WinningNumbers = Regex.Split(cardNumbers.First(), @"\W+").Where(x => !string.IsNullOrEmpty(x)).Select(x => Convert.ToInt32(x)).ToArray();
card.Numbers = Regex.Split(cardNumbers.Last(), @"\W+").Where(x => !string.IsNullOrEmpty(x)).Select(x => Convert.ToInt32(x)).ToArray();
Cards.Add(card);
});
Cards.ForEach(card =>
{
int[] winningNumbers = Regex.Split(cardNumbers.First(), @"\W+").Where(x => !string.IsNullOrEmpty(x)).Select(x => Convert.ToInt32(x)).ToArray();
int[] numbers = Regex.Split(cardNumbers.Last(), @"\W+").Where(x => !string.IsNullOrEmpty(x)).Select(x => Convert.ToInt32(x)).ToArray();
card.ValidNumbers = 0;
card.Numbers!.ToList().ForEach(number => card.ValidNumbers += card.WinningNumbers!.Contains(number) ? 1 : 0);
numbers.ToList().ForEach(number => card.ValidNumbers += winningNumbers.Contains(number) ? 1 : 0);
Cards.Add(card);
});
Console.WriteLine($@"