Skip to content

Commit

Permalink
2018 Day03 performace refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
smabuk committed Nov 7, 2023
1 parent 4f8a74e commit d54bf2a
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions Solutions/2018/Day03.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,17 @@ private static int Solution1(string[] input) {
}

private static int Solution2(string[] input) {
Dictionary<int, HashSet<Point>> Squares = _claims.ToDictionary(c => c.Id, c => c.Squares.ToHashSet());

foreach (int claimId in Squares.Keys.Order()) {
HashSet<Point> squares = Squares[claimId];
List<Point> allOtherSquares = Squares
.Where(c => c.Key != claimId)
.SelectMany(c => c.Value)
.ToList();
if (!squares.Overlaps(allOtherSquares)) {
return claimId;
HashSet<Point> allSquares = _claims
.SelectMany(c => c.Squares)
.GroupBy(square => square)
.Where(g => g.Count() == 1)
.Select(g =>g.Key)
.ToHashSet();

foreach (Claim claim in _claims) {
HashSet<Point> squares = claim.Squares;
if (squares.IsSubsetOf(allSquares)) {
return claim.Id;
}
}

Expand All @@ -50,15 +51,15 @@ private record Claim() : IParsable<Claim> {
public int Id { get; init; }
public Point Start { get; init; }
public (int Wide, int Tall) Size { get; init; }
public List<Point> Squares { get; } = [];
public HashSet<Point> Squares { get; } = [];

[SetsRequiredMembers]
public Claim(int id, Point start, (int Wide, int Tall) size) : this()
{
Id = id;
Start = start;
Size = size;
Squares = GetSquares().ToList();
Squares = GetSquares().ToHashSet();
}

private IEnumerable<Point> GetSquares()
Expand Down

0 comments on commit d54bf2a

Please sign in to comment.