Last active
March 13, 2022 00:57
-
-
Save dviererbe/d1c72d81cef9a4c6980e31478cce5f52 to your computer and use it in GitHub Desktop.
Example Solution for https://exercism.org/tracks/csharp/exercises/dominoes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Collections.Generic; | |
public static class Dominoes | |
{ | |
public static bool CanChain(IEnumerable<(int, int)> dominoes) | |
{ | |
if (dominoes.Any()) | |
return CanChain(dominoes.ToArray().AsSpan()); | |
else | |
return true; | |
} | |
private static bool CanChain(Span<(int DotCount1, int DotCount2)> dominoes) | |
{ | |
var firstDominoFace = dominoes[0]; | |
if (dominoes.Length == 1) | |
{ | |
return firstDominoFace.DotCount1 == firstDominoFace.DotCount2; | |
} | |
for (int i = 1; i < dominoes.Length; ++i) | |
{ | |
var currentDominoFace = dominoes[i]; | |
if (currentDominoFace.DotCount1 == firstDominoFace.DotCount1) | |
{ | |
dominoes[i].DotCount1 = firstDominoFace.DotCount2; | |
if (CanChain(dominoes.Slice(start: 1))) return true; | |
} | |
if (currentDominoFace.DotCount1 == firstDominoFace.DotCount2) | |
{ | |
dominoes[i].DotCount1 = firstDominoFace.DotCount1; | |
if (CanChain(dominoes.Slice(start: 1))) return true; | |
} | |
dominoes[i].DotCount1 = currentDominoFace.DotCount1; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment