feat: add logic for testing game possibility and summing possible games
This commit is contained in:
@@ -1,2 +1,3 @@
|
|||||||
|
global using FluentAssertions;
|
||||||
|
|
||||||
global using Xunit;
|
global using Xunit;
|
||||||
global using FluentAssertions;
|
|
||||||
@@ -1,2 +1,3 @@
|
|||||||
|
global using FluentAssertions;
|
||||||
|
|
||||||
global using Xunit;
|
global using Xunit;
|
||||||
global using FluentAssertions;
|
|
||||||
@@ -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,71 +24,70 @@ public class PuzzleParserTests
|
|||||||
var result = _sut.ParseGame(input);
|
var result = _sut.ParseGame(input);
|
||||||
result.Should().BeEquivalentTo(expected);
|
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 } },
|
public static readonly IEnumerable<object[]> CubeStrings = new List<object[]>
|
||||||
new object[] { "1 green", new Cube { Count = 1, Color = CubeColor.Green } },
|
|
||||||
new object[] { "2 blue", new Cube { Count = 2, Color = CubeColor.Blue } }
|
|
||||||
};
|
|
||||||
|
|
||||||
public static readonly IEnumerable<object[]> ResultStrings = new List<object[]>
|
|
||||||
{
|
|
||||||
new object[]
|
|
||||||
{
|
{
|
||||||
"4 red, 1 green, 2 blue",
|
new object[] { "4 red", new Cube { Count = 4, Color = CubeColor.Red } },
|
||||||
new Result
|
new object[] { "1 green", new Cube { Count = 1, Color = CubeColor.Green } },
|
||||||
{
|
new object[] { "2 blue", new Cube { Count = 2, Color = CubeColor.Blue } }
|
||||||
Cubes =
|
};
|
||||||
[
|
|
||||||
new (){ Count = 4, Color = CubeColor.Red },
|
|
||||||
new (){ Count = 1, Color = CubeColor.Green },
|
|
||||||
new() { Count = 2, Color = CubeColor.Blue }
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
public static readonly IEnumerable<object[]> GameStrings = new List<object[]>
|
public static readonly IEnumerable<object[]> ResultStrings = new List<object[]>
|
||||||
{
|
|
||||||
new object[]
|
|
||||||
{
|
{
|
||||||
"Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green",
|
new object[]
|
||||||
new Game
|
|
||||||
{
|
{
|
||||||
Id = 1,
|
"4 red, 1 green, 2 blue",
|
||||||
Results =
|
new Result
|
||||||
[
|
{
|
||||||
new()
|
Cubes =
|
||||||
{
|
[
|
||||||
Cubes =
|
new (){ Count = 4, Color = CubeColor.Red },
|
||||||
[
|
new (){ Count = 1, Color = CubeColor.Green },
|
||||||
new() { Count = 3, Color = CubeColor.Blue },
|
new() { Count = 2, Color = CubeColor.Blue }
|
||||||
new() { Count = 4, Color = CubeColor.Red }
|
]
|
||||||
]
|
}
|
||||||
},
|
|
||||||
new Result
|
|
||||||
{
|
|
||||||
Cubes =
|
|
||||||
[
|
|
||||||
new() { Count = 1, Color = CubeColor.Red },
|
|
||||||
new() { Count = 2, Color = CubeColor.Green },
|
|
||||||
new() { Count = 6, Color = CubeColor.Blue }
|
|
||||||
]
|
|
||||||
},
|
|
||||||
new Result
|
|
||||||
{
|
|
||||||
Cubes =
|
|
||||||
[
|
|
||||||
new(){ Count = 2, Color = CubeColor.Green }
|
|
||||||
]
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
};
|
|
||||||
|
|
||||||
|
public static readonly IEnumerable<object[]> GameStrings = new List<object[]>
|
||||||
|
{
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
"Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green",
|
||||||
|
new Game
|
||||||
|
{
|
||||||
|
Id = 1,
|
||||||
|
Results =
|
||||||
|
[
|
||||||
|
new()
|
||||||
|
{
|
||||||
|
Cubes =
|
||||||
|
[
|
||||||
|
new() { Count = 3, Color = CubeColor.Blue },
|
||||||
|
new() { Count = 4, Color = CubeColor.Red }
|
||||||
|
]
|
||||||
|
},
|
||||||
|
new Result
|
||||||
|
{
|
||||||
|
Cubes =
|
||||||
|
[
|
||||||
|
new() { Count = 1, Color = CubeColor.Red },
|
||||||
|
new() { Count = 2, Color = CubeColor.Green },
|
||||||
|
new() { Count = 6, Color = CubeColor.Blue }
|
||||||
|
]
|
||||||
|
},
|
||||||
|
new Result
|
||||||
|
{
|
||||||
|
Cubes =
|
||||||
|
[
|
||||||
|
new(){ Count = 2, Color = CubeColor.Green }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -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
|
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
|
public class PuzzleParser
|
||||||
@@ -69,6 +83,10 @@ public class Game
|
|||||||
public class Result
|
public class Result
|
||||||
{
|
{
|
||||||
public List<Cube> Cubes { get; set; } = [];
|
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
|
public class Cube
|
||||||
|
|||||||
Reference in New Issue
Block a user