Compare commits
5 Commits
1214dcadfe
...
35eb89c494
| Author | SHA1 | Date | |
|---|---|---|---|
| 35eb89c494 | |||
| b175cf9812 | |||
| 1487a1cd27 | |||
| 31780b1dca | |||
| 1cce184e86 |
@@ -24,6 +24,10 @@
|
||||
<Content Include="Input\DayThree.txt">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<None Remove="Input\DayFour.txt" />
|
||||
<Content Include="Input\DayFour.txt">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -8,10 +8,13 @@ public class Lobby : IAdventSolution
|
||||
public AdventSolution Solve(string input)
|
||||
{
|
||||
var lines = input.SplitLines();
|
||||
var total = 0;
|
||||
var total = 0UL;
|
||||
var total2 = 0UL;
|
||||
foreach (var line in lines)
|
||||
{
|
||||
var first = '0';
|
||||
total += GreatestXDigits(line, 2);
|
||||
total2 += GreatestXDigits(line, 12);
|
||||
/*var first = '0';
|
||||
var second = '0';
|
||||
for (var i = 0; i < line.Length; i++)
|
||||
{
|
||||
@@ -25,11 +28,41 @@ public class Lobby : IAdventSolution
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -9,4 +9,7 @@ var dayTwoInput = await File.ReadAllTextAsync("./Input/DayTwo.txt");
|
||||
AdventSolver<GiftShop>.Solve(dayTwoInput, 2, "Gift Shop");
|
||||
|
||||
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");
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user