[2023] Update Day4
Signed-off-by: TiagoRG <tiago.rgarcia@ua.pt>
This commit is contained in:
parent
d999774ec1
commit
343f1f08ba
|
@ -7,8 +7,6 @@ public static class Day4
|
||||||
private class Card
|
private class Card
|
||||||
{
|
{
|
||||||
public int CardNumber { get; set; }
|
public int CardNumber { get; set; }
|
||||||
public int[]? WinningNumbers { get; set; }
|
|
||||||
public int[]? Numbers { get; set; }
|
|
||||||
public int ValidNumbers { get; set; }
|
public int ValidNumbers { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,15 +20,11 @@ public static class Day4
|
||||||
Card card = new Card();
|
Card card = new Card();
|
||||||
card.CardNumber = Convert.ToInt32(Regex.Split(line.Split(": ")[0], @"\s+").Last());
|
card.CardNumber = Convert.ToInt32(Regex.Split(line.Split(": ")[0], @"\s+").Last());
|
||||||
string[] cardNumbers = Regex.Split(line.Split(": ")[1], @"\s+\|\s+");
|
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();
|
int[] 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();
|
int[] numbers = Regex.Split(cardNumbers.Last(), @"\W+").Where(x => !string.IsNullOrEmpty(x)).Select(x => Convert.ToInt32(x)).ToArray();
|
||||||
Cards.Add(card);
|
|
||||||
});
|
|
||||||
|
|
||||||
Cards.ForEach(card =>
|
|
||||||
{
|
|
||||||
card.ValidNumbers = 0;
|
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($@"
|
Console.WriteLine($@"
|
||||||
|
|
Loading…
Reference in New Issue