Skip to content

Commit

Permalink
Use ReadOnlySpan<T>
Browse files Browse the repository at this point in the history
- Use `ReadOnlySpan<T>` for arrays so they get more efficient access.
- Remove calls to `Except()` that allocate an array.
  • Loading branch information
martincostello committed Oct 3, 2023
1 parent 8a9ff8e commit 414d97d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
10 changes: 5 additions & 5 deletions src/ProjectEuler/Puzzles/Puzzle031.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ namespace MartinCostello.ProjectEuler.Puzzles;
/// </summary>
public sealed class Puzzle031 : Puzzle
{
/// <summary>
/// The coins, in pence, available in GBP. This field is read-only.
/// </summary>
private static readonly int[] Coins = [1, 2, 5, 10, 20, 50, 100, 200];

/// <inheritdoc />
public override string Question => "How many different ways can the specified amount of money, in pence, be made using any number of coins?";

/// <inheritdoc />
protected override int MinimumArguments => 1;

/// <summary>
/// Gets the coins, in pence, available in GBP.
/// </summary>
private static Span<int> Coins => new int[] { 1, 2, 5, 10, 20, 50, 100, 200 };

/// <inheritdoc />
protected override int SolveCore(string[] args)
{
Expand Down
21 changes: 16 additions & 5 deletions src/ProjectEuler/Puzzles/Puzzle054.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ namespace MartinCostello.ProjectEuler.Puzzles;
/// </summary>
public sealed class Puzzle054 : Puzzle
{
private static readonly char[] Suits = ['C', 'D', 'H', 'S'];
private static readonly char[] Values = ['A', 'K', 'Q', 'J', 'T', '9', '8', '7', '6', '5', '4', '3', '2'];

private delegate bool IsHandPredicate(string[] cards, out char highCard);

/// <inheritdoc />
public override string Question => "How many hands does Player 1 win?";

private static ReadOnlySpan<char> Suits => new char[] { 'C', 'D', 'H', 'S' };

private static ReadOnlySpan<char> Values => new char[] { 'A', 'K', 'Q', 'J', 'T', '9', '8', '7', '6', '5', '4', '3', '2' };

internal static bool IsFirstPlayerTheWinner(string hand)
{
string[] cards = hand.Split(' ');
Expand Down Expand Up @@ -185,8 +186,13 @@ private static bool IsFullHouse(string[] hand, out char highCard)
return false;
}

foreach (char value in Values.Except(new[] { threeValue }))
foreach (char value in Values)
{
if (value == threeValue)
{
continue;
}

if (IsPair(hand, value, out _))
{
highCard = threeValue;
Expand Down Expand Up @@ -236,8 +242,13 @@ private static bool IsTwoPair(string[] hand, out char highCard)
return false;
}

foreach (char value in Values.Except(new[] { firstPairValue }))
foreach (char value in Values)
{
if (value == firstPairValue)
{
continue;
}

if (IsPair(hand, value, out char secondPairValue))
{
highCard = firstPairValue > secondPairValue ? firstPairValue : secondPairValue;
Expand Down

0 comments on commit 414d97d

Please sign in to comment.