diff --git a/Solutions/2018/Day03.cs b/Solutions/2018/Day03.cs index b3e1d81..d29bd26 100644 --- a/Solutions/2018/Day03.cs +++ b/Solutions/2018/Day03.cs @@ -29,16 +29,17 @@ private static int Solution1(string[] input) { } private static int Solution2(string[] input) { - Dictionary> Squares = _claims.ToDictionary(c => c.Id, c => c.Squares.ToHashSet()); - - foreach (int claimId in Squares.Keys.Order()) { - HashSet squares = Squares[claimId]; - List allOtherSquares = Squares - .Where(c => c.Key != claimId) - .SelectMany(c => c.Value) - .ToList(); - if (!squares.Overlaps(allOtherSquares)) { - return claimId; + HashSet 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 squares = claim.Squares; + if (squares.IsSubsetOf(allSquares)) { + return claim.Id; } } @@ -50,7 +51,7 @@ private record Claim() : IParsable { public int Id { get; init; } public Point Start { get; init; } public (int Wide, int Tall) Size { get; init; } - public List Squares { get; } = []; + public HashSet Squares { get; } = []; [SetsRequiredMembers] public Claim(int id, Point start, (int Wide, int Tall) size) : this() @@ -58,7 +59,7 @@ 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 GetSquares()