feat: create parser to parse game into model
This commit is contained in:
@@ -1 +1,2 @@
|
||||
global using Xunit;
|
||||
global using Xunit;
|
||||
global using FluentAssertions;
|
||||
@@ -0,0 +1,94 @@
|
||||
namespace CubeConundrum.Tests;
|
||||
|
||||
public class PuzzleParserTests
|
||||
{
|
||||
private readonly PuzzleParser _sut = new();
|
||||
|
||||
[Theory, MemberData(nameof(TestData.CubeStrings), MemberType = typeof(TestData))]
|
||||
public void ParseCube_GivenAStringThatRepresentsACube_ItShouldReturnTheEquivalentCubeModel(string input, Cube expected)
|
||||
{
|
||||
var result = _sut.ParseCube(input);
|
||||
result.Should().BeEquivalentTo(expected);
|
||||
}
|
||||
|
||||
[Theory, MemberData(nameof(TestData.ResultStrings), MemberType = typeof(TestData))]
|
||||
public void ParseResult_GivenAStringThatRepresentsAResult_ItShouldReturnTheEquivalentResultModel(string input, Result expected)
|
||||
{
|
||||
var result = _sut.ParseResult(input);
|
||||
result.Should().BeEquivalentTo(expected);
|
||||
}
|
||||
|
||||
[Theory, MemberData(nameof(TestData.GameStrings), MemberType = typeof(TestData))]
|
||||
public void ParseGame_GivenAStringThatRepresentsAGame_ItShouldReturnTheEquivalentGameModel(string input, Game expected)
|
||||
{
|
||||
var result = _sut.ParseGame(input);
|
||||
result.Should().BeEquivalentTo(expected);
|
||||
}
|
||||
}
|
||||
|
||||
public static class TestData
|
||||
{
|
||||
public static readonly IEnumerable<object[]> CubeStrings = new List<object[]>
|
||||
{
|
||||
new object[] { "4 red", new Cube { Count = 4, Color = CubeColor.Red } },
|
||||
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 Result
|
||||
{
|
||||
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[]>
|
||||
{
|
||||
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 }
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
namespace CubeConundrum.Tests;
|
||||
|
||||
public class UnitTest1
|
||||
{
|
||||
[Fact]
|
||||
public void Test1()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,2 +1,85 @@
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
Console.WriteLine("Hello, World!");
|
||||
namespace CubeConundrum;
|
||||
|
||||
public class Program
|
||||
{
|
||||
public async static Task<int> Main(string[] args)
|
||||
{
|
||||
return await Task.FromResult(0);
|
||||
}
|
||||
}
|
||||
|
||||
public class PartOnePuzzleSolver
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public class PuzzleParser
|
||||
{
|
||||
public Cube ParseCube(string cubeString)
|
||||
{
|
||||
var parts = cubeString.Split(' ');
|
||||
var count = int.Parse(parts[0]);
|
||||
var color = Enum.Parse<CubeColor>(parts[1], true);
|
||||
|
||||
return new()
|
||||
{
|
||||
Count = count,
|
||||
Color = color
|
||||
};
|
||||
}
|
||||
|
||||
public Result ParseResult(string resultString)
|
||||
{
|
||||
var cubes = resultString
|
||||
.Split(',')
|
||||
.Select(s => s.Trim())
|
||||
.Select(ParseCube)
|
||||
.ToList();
|
||||
|
||||
return new()
|
||||
{
|
||||
Cubes = cubes
|
||||
};
|
||||
}
|
||||
|
||||
public Game ParseGame(string gameString)
|
||||
{
|
||||
var parts = gameString.Split(':');
|
||||
var gameId = int.Parse(parts[0].Split(' ')[1]);
|
||||
var results = parts[1]
|
||||
.Split(';')
|
||||
.Select(s => s.Trim())
|
||||
.Select(ParseResult)
|
||||
.ToList();
|
||||
|
||||
return new()
|
||||
{
|
||||
Id = gameId,
|
||||
Results = results
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public class Game
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public List<Result> Results { get; set; } = [];
|
||||
}
|
||||
|
||||
public class Result
|
||||
{
|
||||
public List<Cube> Cubes { get; set; } = [];
|
||||
}
|
||||
|
||||
public class Cube
|
||||
{
|
||||
public int Count { get; set; }
|
||||
public CubeColor Color { get; set; }
|
||||
}
|
||||
|
||||
public enum CubeColor
|
||||
{
|
||||
Red,
|
||||
Green,
|
||||
Blue
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
# Notes
|
||||
|
||||
## Constraints
|
||||
|
||||
### Total number of cubes: 39
|
||||
|
||||
The elf can't show me more than 39 dice at a time.
|
||||
|
||||
### Total number of each cube type: 12 red, 13 green, 14 blue
|
||||
|
||||
The elf can't show me more than 12 red cubes at a time.
|
||||
The elf can't show me more than 13 green cubes at a time.
|
||||
The elf can't show me more than 14 blue cubes at a time.
|
||||
|
||||
### Types of cubes: red, green, blue
|
||||
|
||||
The elf can only show me red, green, or blue cubes.
|
||||
The elf though can show me any combination of red, green, or blue cubes including none of a particular color or all of a particular color.
|
||||
|
||||
## Structure
|
||||
|
||||
Each game is a line of text.
|
||||
Each game is delimited by "Game N: " where N is the game id.
|
||||
Each game consists of a semi-colon delimited list of sets of cubes.
|
||||
Each set of cubes is a comma delimited list of the number of cubes and the color of the cubes.
|
||||
|
||||
```text
|
||||
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
|
||||
```
|
||||
|
||||
❓ How would I model this?
|
||||
|
||||
```csharp
|
||||
class Game
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public List<Results> Results { get; set; }
|
||||
}
|
||||
|
||||
class Results
|
||||
{
|
||||
public List<Cube> Cubes { get; set; }
|
||||
}
|
||||
|
||||
class Cube
|
||||
{
|
||||
public int Count { get; set; }
|
||||
public CubeColor Color { get; set; }
|
||||
}
|
||||
|
||||
enum CubeColor
|
||||
{
|
||||
Red,
|
||||
Green,
|
||||
Blue
|
||||
}
|
||||
```
|
||||
|
||||
## Problems
|
||||
|
||||
### Problem 1
|
||||
|
||||
🔴 Need to determine which results are possible given the known # of cubes and known # of cubes for each color.
|
||||
|
||||
### Problem 2
|
||||
|
||||
🔴 Need to determine if all the results for a given game are possible
|
||||
|
||||
### Problem 3
|
||||
|
||||
🔴 Need to determine the sum of the game ids for all the possible games
|
||||
|
||||
### Problem 4
|
||||
|
||||
🔴 Need to determine the sum of the game ids for all the possible games
|
||||
Reference in New Issue
Block a user