feat: add logic for testing game possibility and summing possible games
This commit is contained in:
@@ -1,2 +1,3 @@
|
||||
global using Xunit;
|
||||
global using FluentAssertions;
|
||||
|
||||
global using Xunit;
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
global using Xunit;
|
||||
global using FluentAssertions;
|
||||
|
||||
global using Xunit;
|
||||
|
||||
@@ -0,0 +1,159 @@
|
||||
namespace CubeConundrum.Tests;
|
||||
|
||||
public class PartOnePuzzleSolverTests
|
||||
{
|
||||
private readonly PartOnePuzzleSolver _sut = new();
|
||||
|
||||
[Theory, MemberData(nameof(TestData.Results), MemberType = typeof(TestData))]
|
||||
public void IsResultPossible_GivenAResult_ItShouldReturnExpectedOutcome(Result givenResult, bool expected)
|
||||
{
|
||||
var result = _sut.IsResultPossible(givenResult);
|
||||
result.Should().Be(expected);
|
||||
}
|
||||
|
||||
[Theory, MemberData(nameof(TestData.Games), MemberType = typeof(TestData))]
|
||||
public void IsGamePossible_GivenAGame_ItShouldReturnExpectedOutcome(Game givenGame, bool expected)
|
||||
{
|
||||
var result = _sut.IsGamePossible(givenGame);
|
||||
result.Should().Be(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SumPossibleGameIds_GivenAGameCollection_ItShouldReturnTheSumOfTheIdsOfAllPossibleGames()
|
||||
{
|
||||
var games = TestData.Games.Select(o => (Game)o[0]);
|
||||
var result = _sut.SumPossibleGameIds(games);
|
||||
result.Should().Be(1);
|
||||
}
|
||||
|
||||
public static class TestData
|
||||
{
|
||||
public static readonly IEnumerable<object[]> Games = new List<object[]>
|
||||
{
|
||||
new object[]
|
||||
{
|
||||
new Game
|
||||
{
|
||||
Id = 1,
|
||||
Results =
|
||||
{
|
||||
new()
|
||||
{
|
||||
Cubes =
|
||||
{
|
||||
new() { Count = 3, Color = CubeColor.Blue },
|
||||
new() { Count = 4, Color = CubeColor.Red }
|
||||
}
|
||||
},
|
||||
new()
|
||||
{
|
||||
Cubes =
|
||||
{
|
||||
new() { Count = 1, Color = CubeColor.Red },
|
||||
new() { Count = 2, Color = CubeColor.Green },
|
||||
new() { Count = 6, Color = CubeColor.Blue }
|
||||
}
|
||||
},
|
||||
new()
|
||||
{
|
||||
Cubes =
|
||||
{
|
||||
new() { Count = 2, Color = CubeColor.Green }
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
true
|
||||
},
|
||||
new object[]
|
||||
{
|
||||
new Game
|
||||
{
|
||||
Id = 2,
|
||||
Results =
|
||||
{
|
||||
new()
|
||||
{
|
||||
Cubes =
|
||||
{
|
||||
new() { Count = 3, Color = CubeColor.Blue },
|
||||
new() { Count = 4, Color = CubeColor.Red }
|
||||
}
|
||||
},
|
||||
new()
|
||||
{
|
||||
Cubes =
|
||||
{
|
||||
new() { Count = 1, Color = CubeColor.Red },
|
||||
new() { Count = 2, Color = CubeColor.Green },
|
||||
new() { Count = 6, Color = CubeColor.Blue }
|
||||
}
|
||||
},
|
||||
new()
|
||||
{
|
||||
Cubes =
|
||||
{
|
||||
new() { Count = 40, Color = CubeColor.Green }
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
false
|
||||
},
|
||||
};
|
||||
|
||||
public static readonly IEnumerable<object[]> Results = new List<object[]>
|
||||
{
|
||||
new object[]
|
||||
{
|
||||
new Result
|
||||
{
|
||||
Cubes =
|
||||
{
|
||||
new() { Count = 4, Color = CubeColor.Red },
|
||||
new() { Count = 1, Color = CubeColor.Green },
|
||||
new() { Count = 2, Color = CubeColor.Blue }
|
||||
}
|
||||
},
|
||||
true
|
||||
},
|
||||
new object[]
|
||||
{
|
||||
new Result
|
||||
{
|
||||
Cubes =
|
||||
{
|
||||
new() { Count = 13, Color = CubeColor.Red },
|
||||
new() { Count = 14, Color = CubeColor.Green },
|
||||
new() { Count = 14, Color = CubeColor.Blue },
|
||||
}
|
||||
},
|
||||
false
|
||||
},
|
||||
new object[]
|
||||
{
|
||||
new Result
|
||||
{
|
||||
Cubes =
|
||||
{
|
||||
new() { Count = 13, Color = CubeColor.Red },
|
||||
new() { Count = 5, Color = CubeColor.Green },
|
||||
new() { Count = 5, Color = CubeColor.Blue },
|
||||
}
|
||||
},
|
||||
false
|
||||
},
|
||||
new object[]
|
||||
{
|
||||
new Result
|
||||
{
|
||||
Cubes =
|
||||
{
|
||||
new() { Count = 40, Color = CubeColor.Red },
|
||||
}
|
||||
},
|
||||
false
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -24,10 +24,9 @@ public class PuzzleParserTests
|
||||
var result = _sut.ParseGame(input);
|
||||
result.Should().BeEquivalentTo(expected);
|
||||
}
|
||||
}
|
||||
|
||||
public static class TestData
|
||||
{
|
||||
public static class TestData
|
||||
{
|
||||
public static readonly IEnumerable<object[]> CubeStrings = new List<object[]>
|
||||
{
|
||||
new object[] { "4 red", new Cube { Count = 4, Color = CubeColor.Red } },
|
||||
@@ -90,5 +89,5 @@ public static class TestData
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
namespace CubeConundrum.Tests;
|
||||
|
||||
public class ResultTests
|
||||
{
|
||||
[Fact]
|
||||
public void CountProperties_GivenAResult_ItShouldReturnTheCorrectCount()
|
||||
{
|
||||
var result = new Result
|
||||
{
|
||||
Cubes =
|
||||
{
|
||||
new() { Count = 4, Color = CubeColor.Red },
|
||||
new() { Count = 1, Color = CubeColor.Green },
|
||||
new() { Count = 2, Color = CubeColor.Blue }
|
||||
}
|
||||
};
|
||||
|
||||
result.TotalCubeCount.Should().Be(7);
|
||||
result.RedCubeCount.Should().Be(4);
|
||||
result.GreenCubeCount.Should().Be(1);
|
||||
result.BlueCubeCount.Should().Be(2);
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,21 @@ public class Program
|
||||
|
||||
public class PartOnePuzzleSolver
|
||||
{
|
||||
private readonly int _maxCubeCount = 39;
|
||||
private readonly int _maxRedCubeCount = 12;
|
||||
private readonly int _maxGreenCubeCount = 13;
|
||||
private readonly int _maxBlueCubeCount = 14;
|
||||
|
||||
public bool IsResultPossible(Result result) => result.TotalCubeCount <= _maxCubeCount
|
||||
&& result.RedCubeCount <= _maxRedCubeCount
|
||||
&& result.GreenCubeCount <= _maxGreenCubeCount
|
||||
&& result.BlueCubeCount <= _maxBlueCubeCount;
|
||||
|
||||
public bool IsGamePossible(Game game) => game.Results.All(IsResultPossible);
|
||||
|
||||
public int SumPossibleGameIds(IEnumerable<Game> games) => games
|
||||
.Where(IsGamePossible)
|
||||
.Sum(g => g.Id);
|
||||
}
|
||||
|
||||
public class PuzzleParser
|
||||
@@ -69,6 +83,10 @@ public class Game
|
||||
public class Result
|
||||
{
|
||||
public List<Cube> Cubes { get; set; } = [];
|
||||
public int TotalCubeCount => Cubes.Sum(c => c.Count);
|
||||
public int RedCubeCount => Cubes.Where(c => c.Color == CubeColor.Red).Sum(c => c.Count);
|
||||
public int GreenCubeCount => Cubes.Where(c => c.Color == CubeColor.Green).Sum(c => c.Count);
|
||||
public int BlueCubeCount => Cubes.Where(c => c.Color == CubeColor.Blue).Sum(c => c.Count);
|
||||
}
|
||||
|
||||
public class Cube
|
||||
|
||||
Reference in New Issue
Block a user