Compare commits
15 Commits
1214dcadfe
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| a653f07669 | |||
| 1649440a6e | |||
| 86947a6e6d | |||
| 08d495907f | |||
| cf4bc005d5 | |||
| 4a89486898 | |||
| c725c10c0a | |||
| c4ad0b41e8 | |||
| 842e9be900 | |||
| 497bdd7a3b | |||
| 35eb89c494 | |||
| b175cf9812 | |||
| 1487a1cd27 | |||
| 31780b1dca | |||
| 1cce184e86 |
@@ -24,6 +24,22 @@
|
|||||||
<Content Include="Input\DayThree.txt">
|
<Content Include="Input\DayThree.txt">
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
</Content>
|
</Content>
|
||||||
|
<None Remove="Input\DayFour.txt" />
|
||||||
|
<Content Include="Input\DayFour.txt">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</Content>
|
||||||
|
<None Remove="Input\DayFive.txt" />
|
||||||
|
<Content Include="Input\DayFive.txt">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</Content>
|
||||||
|
<None Remove="Input\DaySix.txt" />
|
||||||
|
<Content Include="Input\DaySix.txt">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</Content>
|
||||||
|
<None Remove="Input\DaySeven.txt" />
|
||||||
|
<Content Include="Input\DaySeven.txt">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</Content>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
80
AdventOfCode2025/Cafeteria.cs
Normal file
80
AdventOfCode2025/Cafeteria.cs
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
using AdventOfCode2025.Utils;
|
||||||
|
using Range = AdventOfCode2025.Utils.Range;
|
||||||
|
|
||||||
|
namespace AdventOfCode2025;
|
||||||
|
|
||||||
|
public class Cafeteria : IAdventSolution
|
||||||
|
{
|
||||||
|
public AdventSolution Solve(string input)
|
||||||
|
{
|
||||||
|
var lines = input.SplitLines();
|
||||||
|
|
||||||
|
var ranges = new HashSet<Range>();
|
||||||
|
var startIndex = 0;
|
||||||
|
while (lines[startIndex].Contains('-'))
|
||||||
|
{
|
||||||
|
var line = lines[startIndex++];
|
||||||
|
var parts = line.Split('-');
|
||||||
|
var range = new Range(ulong.Parse(parts[0]), ulong.Parse(parts[1]));
|
||||||
|
AddRange(ranges, range);
|
||||||
|
}
|
||||||
|
|
||||||
|
var fresh = 0;
|
||||||
|
for(; startIndex < lines.Length; startIndex++)
|
||||||
|
{
|
||||||
|
var line = lines[startIndex];
|
||||||
|
if (line.Contains('-'))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
var num = ulong.Parse(line);
|
||||||
|
if(ranges.Any(x => x.IsInRange(num))) fresh++;
|
||||||
|
}
|
||||||
|
RemoveExtraRanges(ranges);
|
||||||
|
var totalFresh = 0UL;
|
||||||
|
foreach (var range in ranges)
|
||||||
|
totalFresh += range.InclusiveCount;
|
||||||
|
|
||||||
|
return new AdventSolution(fresh.ToString(), totalFresh.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void AddRange(HashSet<Range> ranges, Range range)
|
||||||
|
{
|
||||||
|
var first = ranges.FirstOrDefault(x => x.IsInRange(range.Min));
|
||||||
|
if (first != default)
|
||||||
|
{
|
||||||
|
ranges.Remove(first);
|
||||||
|
if(!first.IsInRange(range.Max))
|
||||||
|
first = new Range(first.Min, range.Max);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
first = range;
|
||||||
|
|
||||||
|
var last = ranges.LastOrDefault(x => x.IsInRange(first.Max));
|
||||||
|
if (last != default)
|
||||||
|
{
|
||||||
|
ranges.Remove(last);
|
||||||
|
if (!last.IsInRange(first.Min))
|
||||||
|
last = new Range(first.Min, last.Max);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
last = first;
|
||||||
|
|
||||||
|
ranges.Add(last);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void RemoveExtraRanges(HashSet<Range> ranges)
|
||||||
|
{
|
||||||
|
var toRemove = new List<Range>();
|
||||||
|
foreach (var range in ranges)
|
||||||
|
{
|
||||||
|
if(ranges.Any(x => x != range && (x.IsInRange(range.Min) || x.IsInRange(range.Max))))
|
||||||
|
toRemove.Add(range);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var remove in toRemove)
|
||||||
|
{
|
||||||
|
ranges.Remove(remove);
|
||||||
|
AddRange(ranges, remove);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
using Range = AdventOfCode2025.Utils.Range;
|
||||||
|
|
||||||
namespace AdventOfCode2025;
|
namespace AdventOfCode2025;
|
||||||
|
|
||||||
@@ -44,5 +45,3 @@ public class GiftShop : IAdventSolution
|
|||||||
return new Range(ulong.Parse(numbers[0]), ulong.Parse(numbers[^1]));
|
return new Range(ulong.Parse(numbers[0]), ulong.Parse(numbers[^1]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public record struct Range(ulong Min, ulong Max);
|
|
||||||
32
AdventOfCode2025/Laboratories.cs
Normal file
32
AdventOfCode2025/Laboratories.cs
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
using AdventOfCode2025.Utils;
|
||||||
|
|
||||||
|
namespace AdventOfCode2025;
|
||||||
|
|
||||||
|
public class Laboratories : IAdventSolution
|
||||||
|
{
|
||||||
|
public AdventSolution Solve(string input)
|
||||||
|
{
|
||||||
|
var lines = input.SplitLines();
|
||||||
|
var start = lines[0].IndexOf('S');
|
||||||
|
var splitters = new HashSet<Coordinate>();
|
||||||
|
var origin = new Coordinate(start, 0);
|
||||||
|
Traverse(lines, origin, splitters);
|
||||||
|
return new AdventSolution(splitters.Count.ToString(), null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Traverse(string[] map, Coordinate start, HashSet<Coordinate> splitters)
|
||||||
|
{
|
||||||
|
if (start.X < 0 || start.X >= map[0].Length)
|
||||||
|
return;
|
||||||
|
|
||||||
|
for (var y = start.Y; y < map.Length; y++)
|
||||||
|
{
|
||||||
|
if (map[y][start.X] != '^') continue;
|
||||||
|
if (!splitters.Add(new Coordinate(start.X, y))) return;
|
||||||
|
|
||||||
|
Traverse(map, new Coordinate(start.X + 1, y), splitters);
|
||||||
|
Traverse(map, new Coordinate(start.X - 1, y), splitters);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,10 +8,13 @@ public class Lobby : IAdventSolution
|
|||||||
public AdventSolution Solve(string input)
|
public AdventSolution Solve(string input)
|
||||||
{
|
{
|
||||||
var lines = input.SplitLines();
|
var lines = input.SplitLines();
|
||||||
var total = 0;
|
var total = 0UL;
|
||||||
|
var total2 = 0UL;
|
||||||
foreach (var line in lines)
|
foreach (var line in lines)
|
||||||
{
|
{
|
||||||
var first = '0';
|
total += GreatestXDigits(line, 2);
|
||||||
|
total2 += GreatestXDigits(line, 12);
|
||||||
|
/*var first = '0';
|
||||||
var second = '0';
|
var second = '0';
|
||||||
for (var i = 0; i < line.Length; i++)
|
for (var i = 0; i < line.Length; i++)
|
||||||
{
|
{
|
||||||
@@ -25,11 +28,41 @@ public class Lobby : IAdventSolution
|
|||||||
second = line[i];
|
second = line[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
total += int.Parse($"{first}{second}");*/
|
||||||
|
|
||||||
total += int.Parse($"{first}{second}");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new AdventSolution(total.ToString(), null);
|
return new AdventSolution(total.ToString(), total2.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
private readonly List<char> _chars = new();
|
||||||
|
|
||||||
|
private ulong GreatestXDigits(string input, int digits)
|
||||||
|
{
|
||||||
|
_chars.Clear();
|
||||||
|
_chars.Add(input[0]);
|
||||||
|
for (var i = 1; i < input.Length; i++)
|
||||||
|
{
|
||||||
|
var remainingChars = input.Length - i - 1;
|
||||||
|
var c = input[i];
|
||||||
|
if(!IsReplace(c, remainingChars, digits) && _chars.Count < digits)
|
||||||
|
{
|
||||||
|
_chars.Add(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ulong.Parse(string.Join("", _chars));
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool IsReplace(char c, int remaining, int digits)
|
||||||
|
{
|
||||||
|
for (var i = 0; i < _chars.Count; i++)
|
||||||
|
{
|
||||||
|
if (c <= _chars[i] || (remaining < digits - (i+1))) continue;
|
||||||
|
_chars.RemoveRange(i, _chars.Count - i);
|
||||||
|
_chars.Add(c);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
57
AdventOfCode2025/PrintingDepartment.cs
Normal file
57
AdventOfCode2025/PrintingDepartment.cs
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
using AdventOfCode2025.Utils;
|
||||||
|
|
||||||
|
namespace AdventOfCode2025;
|
||||||
|
|
||||||
|
public class PrintingDepartment : IAdventSolution
|
||||||
|
{
|
||||||
|
private readonly HashSet<Coordinate> _existingRolls = new();
|
||||||
|
public AdventSolution Solve(string input)
|
||||||
|
{
|
||||||
|
var map = input.SplitLines();
|
||||||
|
for (var i = 0; i < map.Length; i++)
|
||||||
|
{
|
||||||
|
for (var j = 0; j < map[i].Length; j++)
|
||||||
|
{
|
||||||
|
if(map[i][j] != '.')
|
||||||
|
_existingRolls.Add(new Coordinate(j, i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var initialCount = _existingRolls.Count;
|
||||||
|
var accessible = -1;
|
||||||
|
var firstAccessible = -1;
|
||||||
|
var toRemove = new HashSet<Coordinate>();
|
||||||
|
while(accessible != 0 && _existingRolls.Count > 0)
|
||||||
|
{
|
||||||
|
accessible = 0;
|
||||||
|
foreach (var c in _existingRolls)
|
||||||
|
{
|
||||||
|
if (!IsAccessible(c)) continue;
|
||||||
|
accessible++;
|
||||||
|
toRemove.Add(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (firstAccessible == -1) firstAccessible = accessible;
|
||||||
|
|
||||||
|
_existingRolls.RemoveWhere(x => toRemove.Contains(x));
|
||||||
|
toRemove.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
return new AdventSolution(firstAccessible.ToString(), (initialCount - _existingRolls.Count).ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
private readonly Coordinate[] _directions =
|
||||||
|
[
|
||||||
|
new(-1, -1), new( 0, -1), new( 1, -1),
|
||||||
|
new(-1, 0), new( 1, 0),
|
||||||
|
new(-1, 1), new( 0, 1), new( 1, 1)
|
||||||
|
];
|
||||||
|
private bool IsAccessible(Coordinate position)
|
||||||
|
{
|
||||||
|
var neighbors = 0;
|
||||||
|
foreach (var dir in _directions)
|
||||||
|
{
|
||||||
|
if(_existingRolls.Contains(position + dir)) neighbors++;
|
||||||
|
}
|
||||||
|
return neighbors < 4;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,3 +10,15 @@ AdventSolver<GiftShop>.Solve(dayTwoInput, 2, "Gift Shop");
|
|||||||
|
|
||||||
var dayThreeInput = await File.ReadAllTextAsync("./Input/DayThree.txt");
|
var dayThreeInput = await File.ReadAllTextAsync("./Input/DayThree.txt");
|
||||||
AdventSolver<Lobby>.Solve(dayThreeInput, 3, "Lobby");
|
AdventSolver<Lobby>.Solve(dayThreeInput, 3, "Lobby");
|
||||||
|
|
||||||
|
var dayFourInput = await File.ReadAllTextAsync("./Input/DayFour.txt");
|
||||||
|
AdventSolver<PrintingDepartment>.Solve(dayFourInput, 4, "Printing Department");
|
||||||
|
|
||||||
|
var dayFiveInput = await File.ReadAllTextAsync("./Input/DayFive.txt");
|
||||||
|
AdventSolver<Cafeteria>.Solve(dayFiveInput, 5, "Cafeteria");
|
||||||
|
|
||||||
|
var daySixInput = await File.ReadAllTextAsync("./Input/DaySix.txt");
|
||||||
|
AdventSolver<TrashCompactor>.Solve(daySixInput, 6, "Trash Compactor");
|
||||||
|
|
||||||
|
var daySevenInput = await File.ReadAllTextAsync("./Input/DaySeven.txt");
|
||||||
|
AdventSolver<Laboratories>.Solve(daySevenInput, 7, "Laboratories");
|
||||||
34
AdventOfCode2025/TrashCompactor.cs
Normal file
34
AdventOfCode2025/TrashCompactor.cs
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
using AdventOfCode2025.Utils;
|
||||||
|
|
||||||
|
namespace AdventOfCode2025;
|
||||||
|
|
||||||
|
public class TrashCompactor : IAdventSolution
|
||||||
|
{
|
||||||
|
public AdventSolution Solve(string input)
|
||||||
|
{
|
||||||
|
var lines = input.SplitLines();
|
||||||
|
var operators = lines[^1].Split(' ', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
||||||
|
var results = new ulong[operators.Length];
|
||||||
|
for (var i = 0; i < lines.Length - 1; i++)
|
||||||
|
{
|
||||||
|
var line = lines[i].Split(' ', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
||||||
|
for (var j = 0; j < line.Length; j++)
|
||||||
|
{
|
||||||
|
if(i == 0)
|
||||||
|
results[j] = ulong.Parse(line[j]);
|
||||||
|
else switch (operators[j])
|
||||||
|
{
|
||||||
|
case "+":
|
||||||
|
results[j] += ulong.Parse(line[j]);
|
||||||
|
break;
|
||||||
|
case "*":
|
||||||
|
results[j] *= ulong.Parse(line[j]);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var sum = results.Aggregate(0UL, (current, result) => current + result);
|
||||||
|
|
||||||
|
return new AdventSolution(sum.ToString(),null);
|
||||||
|
}
|
||||||
|
}
|
||||||
16
AdventOfCode2025/Utils/Coordinate.cs
Normal file
16
AdventOfCode2025/Utils/Coordinate.cs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
using System.Numerics;
|
||||||
|
|
||||||
|
namespace AdventOfCode2025.Utils;
|
||||||
|
|
||||||
|
public record struct Coordinate(int X, int Y) : IAdditionOperators<Coordinate,Coordinate,Coordinate>, ISubtractionOperators<Coordinate,Coordinate,Coordinate>
|
||||||
|
{
|
||||||
|
public static Coordinate operator +(Coordinate left, Coordinate right)
|
||||||
|
{
|
||||||
|
return new Coordinate(left.X + right.X, left.Y + right.Y);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Coordinate operator -(Coordinate left, Coordinate right)
|
||||||
|
{
|
||||||
|
return new Coordinate(left.X - right.X, left.Y - right.Y);
|
||||||
|
}
|
||||||
|
}
|
||||||
8
AdventOfCode2025/Utils/Range.cs
Normal file
8
AdventOfCode2025/Utils/Range.cs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
namespace AdventOfCode2025.Utils;
|
||||||
|
|
||||||
|
public record struct Range(ulong Min, ulong Max)
|
||||||
|
{
|
||||||
|
public ulong InclusiveCount => Max - Min + 1;
|
||||||
|
|
||||||
|
public bool IsInRange(ulong value) => value >= Min && value <= Max;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user