-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleCardShuffler.cs
67 lines (61 loc) · 1.42 KB
/
SimpleCardShuffler.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
namespace AdventOfCode.Year2019.Day22;
public class SimpleCardShuffler : ICardShuffler
{
private List<int> _deck;
public SimpleCardShuffler(int numberOfCards)
{
if (numberOfCards <= 0)
{
throw new ArgumentOutOfRangeException(nameof(numberOfCards), "Number of cards must be positive.");
}
_deck = Enumerable.Range(0, numberOfCards).ToList();
}
public void DealIntoNewStack()
{
_deck.Reverse();
}
public void CutN(int n)
{
if (n > 0)
{
List<int> cut = _deck.GetRange(0, n);
_deck.RemoveRange(0, n);
_deck.AddRange(cut);
}
else if (n < 0)
{
n = -n;
List<int> cut = _deck.GetRange(_deck.Count - n, n);
_deck.RemoveRange(_deck.Count - n, n);
_deck.InsertRange(0, cut);
}
}
public void DealWithIncrementN(int n)
{
List<int> newDeck = Enumerable.Repeat(-1, _deck.Count).ToList();
int index = 0;
foreach (int card in _deck)
{
if (newDeck[index] >= 0)
{
throw new InvalidOperationException("Deck is not properly shuffled.");
}
newDeck[index] = card;
index = (index + n) % newDeck.Count;
}
if (newDeck.Skip(1).Any(c => c < 0))
{
throw new InvalidOperationException("Deck is not properly shuffled.");
}
_deck = newDeck.ToList();
}
public long GetCardIndex(int cardNumber)
{
int index = _deck.IndexOf(cardNumber);
if (index < 0)
{
throw new ArgumentException($"Card {cardNumber} not found in deck.", nameof(cardNumber));
}
return index;
}
}