-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay05.cs
More file actions
64 lines (57 loc) · 2.15 KB
/
Day05.cs
File metadata and controls
64 lines (57 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System.Diagnostics;
using System;
namespace _2025._05
{
public sealed class Day05 : Base
{
public Day05(bool example) : base(example)
{
Day = "05";
}
private (List<(ulong, ulong)>, IEnumerable<ulong> ingredients) ParseInput()
{
string[] input = ReadInput();
var ranges = input.TakeWhile(line => !string.IsNullOrWhiteSpace(line)).Select(x =>
{
var span = x.AsSpan();
int dash = span.IndexOf('-');
return (ulong.Parse(span[..dash]), ulong.Parse(span[(dash+1)..]));
}).OrderBy(x => x.Item1).ThenBy(x => x.Item2).ToList();
for (int currRange = 0; currRange < ranges.Count; currRange++)
{
var (lower, upper) = ranges[currRange];
int nextRange = currRange + 1;
while(nextRange < ranges.Count)
{
(ulong nextLower, ulong nextUpper) = ranges[nextRange];
if (nextLower >= ranges[currRange].Item1 && nextLower <= ranges[currRange].Item2)
{
ranges[currRange] = (ranges[currRange].Item1, Math.Max(ranges[currRange].Item2, nextUpper));
ranges.RemoveAt(nextRange);
continue;
}
nextRange++;
}
}
var ingredients = input
.SkipWhile(line => !string.IsNullOrWhiteSpace(line))
.Skip(1)
.Select(ulong.Parse);
return (ranges, ingredients);
}
public override object PartOne()
{
var (ranges, ingredients) = ParseInput();
return ingredients.Count(ingredient =>
ranges.Any(r => ingredient >= r.Item1 && ingredient <= r.Item2)
);
}
public override object PartTwo()
{
var (ranges, _) = ParseInput();
return ranges.Aggregate< (ulong, ulong), ulong >(
0, (acc, x) => acc + (x.Item2 - x.Item1 +1)
);;
}
}
}