my advent of code solutions
1namespace Solutions._2018;
2
3/// <summary>
4/// Day 3: <a href="https://adventofcode.com/2018/day/3"/>
5/// </summary>
6public sealed partial class Day03NoMatterHowYouSliceIt() : Day(2018, 3, "No Matter How You Slice It")
7{
8 private List<Claim>? _claims;
9 private readonly Dictionary<(int x, int y), List<int>> _plots = [];
10
11 [GeneratedRegex(@"\d+")]
12 private static partial Regex Digits();
13
14 private record Claim(int ID, int X, int Y, int SizeX, int SizeY);
15
16 public override void ProcessInput()
17 {
18 _claims = Input.Select(line => Digits().Matches(line).Select(m => int.Parse(m.Value)).ToList())
19 .Select(l => new Claim(l[0], l[1], l[2], l[3], l[4])).ToList();
20
21 foreach (var claim in _claims)
22 {
23 foreach (var y in Enumerable.Range(claim.X, claim.SizeX))
24 foreach (var x in Enumerable.Range(claim.Y, claim.SizeY))
25 {
26 if (!_plots.ContainsKey((x, y))) _plots.Add((x, y), []);
27 _plots[(x, y)].Add(claim.ID);
28 }
29 }
30 }
31
32 public override object Part1() => _plots.Values.Count(v => v.Count > 1);
33 public override object Part2()
34 {
35 var overlapping = _plots.Where(p => p.Value.Count > 1).SelectMany(p => p.Value).Distinct().ToList();
36 return _claims!.Single(p => !overlapping.Contains(p.ID)).ID;
37 }
38}