Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ReadOnlySpan<T> #298

Merged
merged 1 commit into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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