2021-12-06 00:36:45 +00:00
|
|
|
|
namespace aoc2021;
|
2021-12-05 18:32:23 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Day 5: <see href="https://adventofcode.com/2021/day/5"/>
|
|
|
|
|
/// </summary>
|
|
|
|
|
public sealed class Day05 : Day
|
|
|
|
|
{
|
|
|
|
|
public Day05() : base(5, "Hydrothermal Venture")
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-06 00:36:45 +00:00
|
|
|
|
private int Solve(bool diagonals = false) =>
|
|
|
|
|
Input
|
2021-12-06 00:52:45 +00:00
|
|
|
|
.Select(s => Regex.Split(s, @"\D+").Select(int.Parse).ToList())
|
2021-12-06 00:36:45 +00:00
|
|
|
|
.Where(t => diagonals || t[0] == t[2] || t[1] == t[3])
|
|
|
|
|
.SelectMany(t =>
|
|
|
|
|
Enumerable.Range(0, Math.Max(Math.Abs(t[0] - t[2]), Math.Abs(t[1] - t[3])) + 1)
|
|
|
|
|
.Select(i => (
|
|
|
|
|
t[0] > t[2] ? t[2] + i : t[0] < t[2] ? t[2] - i : t[2],
|
|
|
|
|
t[1] > t[3] ? t[3] + i : t[1] < t[3] ? t[3] - i : t[3])))
|
|
|
|
|
.GroupBy(k => k)
|
|
|
|
|
.Count(k => k.Count() > 1);
|
|
|
|
|
|
|
|
|
|
public override string Part1() => $"{Solve()}";
|
|
|
|
|
|
|
|
|
|
public override string Part2() => $"{Solve(diagonals: true)}";
|
2021-12-05 18:32:23 +00:00
|
|
|
|
}
|