feat: Add day 5
This commit is contained in:
parent
d4b2087892
commit
1a4e35b61c
3 changed files with 1250 additions and 0 deletions
56
AdventOfCode2025/Day5.cs
Normal file
56
AdventOfCode2025/Day5.cs
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
namespace AdventOfCode2025;
|
||||
|
||||
public class Day5 : Solution
|
||||
{
|
||||
public override string SolvePart1()
|
||||
{
|
||||
List<string> lines = InputReader.ReadLines(5).ToList();
|
||||
List<Range> freshRanges = GetFreshRanges(lines);
|
||||
int freshIngredients = lines[(freshRanges.Count + 1)..].Select(long.Parse).Count(id => IsFresh(id, freshRanges));
|
||||
return freshIngredients.ToString();
|
||||
}
|
||||
|
||||
public override string SolvePart2()
|
||||
{
|
||||
List<string> lines = InputReader.ReadLines(5).ToList();
|
||||
List<Range> ranges = GetFreshRanges(lines).OrderBy(x => x.From).ThenBy(x => x.To).ToList();
|
||||
|
||||
var merged = new List<Range>();
|
||||
Range current = ranges[0];
|
||||
|
||||
for (var i = 1; i < ranges.Count; i++)
|
||||
{
|
||||
Range next = ranges[i];
|
||||
|
||||
if (next.From <= current.To)
|
||||
{
|
||||
current.To = Math.Max(current.To, next.To);
|
||||
}
|
||||
else
|
||||
{
|
||||
merged.Add(current);
|
||||
current = next;
|
||||
}
|
||||
}
|
||||
|
||||
merged.Add(current);
|
||||
|
||||
return merged.Sum(r => r.To - r.From + 1).ToString();
|
||||
}
|
||||
|
||||
private static List<Range> GetFreshRanges(List<string> lines) => lines
|
||||
.TakeWhile(line => !string.IsNullOrEmpty(line))
|
||||
.Select(range => range.Split('-').Select(long.Parse).ToArray())
|
||||
.Select(v => new Range(v))
|
||||
.ToList();
|
||||
|
||||
private static bool IsFresh(long id, IEnumerable<Range> ranges) => ranges.Any(range => IsFresh(id, range));
|
||||
|
||||
private static bool IsFresh(long id, Range range) => id >= range.From && id <= range.To;
|
||||
|
||||
private sealed class Range(long[] values)
|
||||
{
|
||||
public long From { get; } = values[0];
|
||||
public long To { get; set; } = values[1];
|
||||
}
|
||||
}
|
||||
11
AdventOfCode2025/Inputs/Day5_0.txt
Normal file
11
AdventOfCode2025/Inputs/Day5_0.txt
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
3-5
|
||||
10-14
|
||||
16-20
|
||||
12-18
|
||||
|
||||
1
|
||||
5
|
||||
8
|
||||
11
|
||||
17
|
||||
32
|
||||
1183
AdventOfCode2025/Inputs/Day5_1.txt
Normal file
1183
AdventOfCode2025/Inputs/Day5_1.txt
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue