I thought the most obvious way to do this would be to intersect a bunch of HashSets, but seemed to be more short & sweet to just check that every character in the first line was contained in every other line 🤷
public class Part1 : Puzzle<IEnumerable<PlaneGroup>, int> { public override int SampleAnswer => 11; public override IEnumerable<PlaneGroup> ParseInput(string rawInput) => rawInput .Split(Environment.NewLine + Environment.NewLine) .Where(line => line.Length > 0) .Select(group => new PlaneGroup( group .Split(Environment.NewLine) .Where(x => x.Length > 0))); public override int Solve(IEnumerable<PlaneGroup> input) => input.Sum(x => x.CountDistinctAnswers()); }
public class Part2 : Part1 { public override int SampleAnswer => 6; public override int Solve(IEnumerable<PlaneGroup> input) => input.Sum(x => x.CountIntersectingAnswers()); }
public class PlaneGroup { List<string> answers; public PlaneGroup(IEnumerable<string> answers) { this.answers = answers.ToList(); } public int CountDistinctAnswers() => new HashSet<char>(answers.SelectMany(a => a)) .Count(); public int CountIntersectingAnswers() => answers.First() .Count(c => answers.All(a => a.Contains(c))); }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I thought the most obvious way to do this would be to intersect a bunch of HashSets, but seemed to be more short & sweet to just check that every character in the first line was contained in every other line 🤷
Part 1
Part 2
PlaneGroup