Apply nullability
This commit is contained in:
@@ -3,8 +3,8 @@
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<RootNamespace>AdventOfCode.Common</RootNamespace>
|
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
|
<RootNamespace>AdventOfCode.Common</RootNamespace>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -3,9 +3,11 @@
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
<RootNamespace>AdventOfCode2017.Tests</RootNamespace>
|
<RootNamespace>AdventOfCode2017.Tests</RootNamespace>
|
||||||
|
|
||||||
<IsPackable>false</IsPackable>
|
<IsPackable>false</IsPackable>
|
||||||
|
<IsTestProject>true</IsTestProject>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
<RootNamespace>AdventOfCode2017</RootNamespace>
|
<RootNamespace>AdventOfCode2017</RootNamespace>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
|||||||
@@ -30,11 +30,11 @@ public class Day03 : IDay
|
|||||||
{
|
{
|
||||||
public string ResolvePart1(string[] inputs)
|
public string ResolvePart1(string[] inputs)
|
||||||
{
|
{
|
||||||
return null;
|
return string.Empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string ResolvePart2(string[] inputs)
|
public string ResolvePart2(string[] inputs)
|
||||||
{
|
{
|
||||||
return null;
|
return string.Empty;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,9 +3,11 @@
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
<RootNamespace>AdventOfCode2018.Tests</RootNamespace>
|
<RootNamespace>AdventOfCode2018.Tests</RootNamespace>
|
||||||
|
|
||||||
<IsPackable>false</IsPackable>
|
<IsPackable>false</IsPackable>
|
||||||
|
<IsTestProject>true</IsTestProject>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -7,28 +7,28 @@ public class Day06_Tests
|
|||||||
[Fact]
|
[Fact]
|
||||||
public void ChronoPoint_FromString__Test1()
|
public void ChronoPoint_FromString__Test1()
|
||||||
{
|
{
|
||||||
Day06.ChronoPoint point = Day06.ChronoPoint.FromString("1, 1");
|
Day06.ChronoPoint? point = Day06.ChronoPoint.FromString("1, 1");
|
||||||
|
|
||||||
Assert.Equal(1, point.X);
|
Assert.Equal(1, point?.X);
|
||||||
Assert.Equal(1, point.Y);
|
Assert.Equal(1, point?.Y);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void ChronoPoint_FromString__Test2()
|
public void ChronoPoint_FromString__Test2()
|
||||||
{
|
{
|
||||||
Day06.ChronoPoint point = Day06.ChronoPoint.FromString("1, 6");
|
Day06.ChronoPoint? point = Day06.ChronoPoint.FromString("1, 6");
|
||||||
|
|
||||||
Assert.Equal(1, point.X);
|
Assert.Equal(1, point?.X);
|
||||||
Assert.Equal(6, point.Y);
|
Assert.Equal(6, point?.Y);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void ChronoPoint_FromString__Test3()
|
public void ChronoPoint_FromString__Test3()
|
||||||
{
|
{
|
||||||
Day06.ChronoPoint point = Day06.ChronoPoint.FromString("8, 9");
|
Day06.ChronoPoint? point = Day06.ChronoPoint.FromString("8, 9");
|
||||||
|
|
||||||
Assert.Equal(8, point.X);
|
Assert.Equal(8, point?.X);
|
||||||
Assert.Equal(9, point.Y);
|
Assert.Equal(9, point?.Y);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion ChronoPoint_FromString
|
#endregion ChronoPoint_FromString
|
||||||
@@ -38,9 +38,12 @@ public class Day06_Tests
|
|||||||
[Fact]
|
[Fact]
|
||||||
public void ChronoPoint_ManhattanDistance__Test1()
|
public void ChronoPoint_ManhattanDistance__Test1()
|
||||||
{
|
{
|
||||||
Day06.ChronoPoint p0 = Day06.ChronoPoint.FromString("8, 9");
|
Day06.ChronoPoint? p0 = Day06.ChronoPoint.FromString("8, 9");
|
||||||
Day06.ChronoPoint p1 = Day06.ChronoPoint.FromString("1, 6");
|
Day06.ChronoPoint? p1 = Day06.ChronoPoint.FromString("1, 6");
|
||||||
|
|
||||||
|
Assert.NotNull(p0);
|
||||||
|
Assert.NotNull(p1);
|
||||||
|
|
||||||
int distance = Day06.ChronoPoint.ManhattanDistance(p0, p1);
|
int distance = Day06.ChronoPoint.ManhattanDistance(p0, p1);
|
||||||
|
|
||||||
Assert.Equal(10, distance);
|
Assert.Equal(10, distance);
|
||||||
@@ -49,9 +52,12 @@ public class Day06_Tests
|
|||||||
[Fact]
|
[Fact]
|
||||||
public void ChronoPoint_ManhattanDistance__Test2()
|
public void ChronoPoint_ManhattanDistance__Test2()
|
||||||
{
|
{
|
||||||
Day06.ChronoPoint p0 = Day06.ChronoPoint.FromString("1, 1");
|
Day06.ChronoPoint? p0 = Day06.ChronoPoint.FromString("1, 1");
|
||||||
Day06.ChronoPoint p1 = Day06.ChronoPoint.FromString("1, 6");
|
Day06.ChronoPoint? p1 = Day06.ChronoPoint.FromString("1, 6");
|
||||||
|
|
||||||
|
Assert.NotNull(p0);
|
||||||
|
Assert.NotNull(p1);
|
||||||
|
|
||||||
int distance = Day06.ChronoPoint.ManhattanDistance(p0, p1);
|
int distance = Day06.ChronoPoint.ManhattanDistance(p0, p1);
|
||||||
|
|
||||||
Assert.Equal(5, distance);
|
Assert.Equal(5, distance);
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
<RootNamespace>AdventOfCode2018</RootNamespace>
|
<RootNamespace>AdventOfCode2018</RootNamespace>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ public class Day01 : IDay
|
|||||||
public string ResolvePart2(string[] inputs)
|
public string ResolvePart2(string[] inputs)
|
||||||
{
|
{
|
||||||
int accumulator = 0;
|
int accumulator = 0;
|
||||||
List<int> accumulatorHistory = new();
|
List<int> accumulatorHistory = [];
|
||||||
int? repeatedAccumulator = null;
|
int? repeatedAccumulator = null;
|
||||||
while (repeatedAccumulator == null)
|
while (repeatedAccumulator == null)
|
||||||
{
|
{
|
||||||
@@ -108,6 +108,6 @@ public class Day01 : IDay
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return repeatedAccumulator.ToString();
|
return repeatedAccumulator.ToString() ?? string.Empty;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -94,7 +94,7 @@ public class Day03 : IDay
|
|||||||
{
|
{
|
||||||
List<Claim> claims = inputs.Select(Claim.FromString).ToList();
|
List<Claim> claims = inputs.Select(Claim.FromString).ToList();
|
||||||
|
|
||||||
Claim unOverlappingClaim = null;
|
Claim? unOverlappingClaim = null;
|
||||||
for (int i = 0; i < claims.Count; i++)
|
for (int i = 0; i < claims.Count; i++)
|
||||||
{
|
{
|
||||||
bool overlaps = false;
|
bool overlaps = false;
|
||||||
@@ -113,7 +113,7 @@ public class Day03 : IDay
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return unOverlappingClaim.ID.ToString();
|
return unOverlappingClaim?.ID.ToString() ?? string.Empty;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ public class Day04 : IDay
|
|||||||
Dictionary<int, GuardSleepHistogram> dictFullHistogram = BuildFullHistogram(guardEvents);
|
Dictionary<int, GuardSleepHistogram> dictFullHistogram = BuildFullHistogram(guardEvents);
|
||||||
|
|
||||||
// Find sleepier guard
|
// Find sleepier guard
|
||||||
GuardSleepHistogram highestSleeperHistogram = null;
|
GuardSleepHistogram? highestSleeperHistogram = null;
|
||||||
long highestTotalSleep = long.MinValue;
|
long highestTotalSleep = long.MinValue;
|
||||||
foreach (GuardSleepHistogram guardHistogram in dictFullHistogram.Values)
|
foreach (GuardSleepHistogram guardHistogram in dictFullHistogram.Values)
|
||||||
{
|
{
|
||||||
@@ -83,6 +83,7 @@ public class Day04 : IDay
|
|||||||
highestTotalSleep = totalSleep;
|
highestTotalSleep = totalSleep;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (highestSleeperHistogram == null) { return string.Empty; }
|
||||||
|
|
||||||
// Find sleepier minute
|
// Find sleepier minute
|
||||||
int maxSleepMinute = int.MinValue;
|
int maxSleepMinute = int.MinValue;
|
||||||
@@ -156,14 +157,12 @@ public class Day04 : IDay
|
|||||||
|
|
||||||
foreach (GuardSleepHistogram dayGuardHistogram in dictDayHistogram.Values)
|
foreach (GuardSleepHistogram dayGuardHistogram in dictDayHistogram.Values)
|
||||||
{
|
{
|
||||||
if (dictFullHistogram.TryGetValue(dayGuardHistogram.ID, out GuardSleepHistogram guardHistogram))
|
if (dictFullHistogram.TryGetValue(dayGuardHistogram.ID, out GuardSleepHistogram? guardHistogram))
|
||||||
{
|
{
|
||||||
guardHistogram.AddHistogram(dayGuardHistogram);
|
guardHistogram.AddHistogram(dayGuardHistogram);
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
else
|
dictFullHistogram.Add(dayGuardHistogram.ID, dayGuardHistogram);
|
||||||
{
|
|
||||||
dictFullHistogram.Add(dayGuardHistogram.ID, dayGuardHistogram);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -97,6 +97,7 @@ public class Day06 : IDay
|
|||||||
return inputs
|
return inputs
|
||||||
.Where(input => string.IsNullOrEmpty(input) == false)
|
.Where(input => string.IsNullOrEmpty(input) == false)
|
||||||
.Select(ChronoPoint.FromString)
|
.Select(ChronoPoint.FromString)
|
||||||
|
.OfType<ChronoPoint>()
|
||||||
.ToList();
|
.ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -197,7 +198,7 @@ public class Day06 : IDay
|
|||||||
public int X { get; set; }
|
public int X { get; set; }
|
||||||
public int Y { get; set; }
|
public int Y { get; set; }
|
||||||
|
|
||||||
public static ChronoPoint FromString(string strPoint)
|
public static ChronoPoint? FromString(string strPoint)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(strPoint)) { return null; }
|
if (string.IsNullOrEmpty(strPoint)) { return null; }
|
||||||
string[] parts = strPoint.Split([", "], StringSplitOptions.RemoveEmptyEntries);
|
string[] parts = strPoint.Split([", "], StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
|||||||
@@ -128,9 +128,9 @@ public class Day07 : IDay
|
|||||||
return totalElapsedTime.ToString();
|
return totalElapsedTime.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public class InstructionNode
|
public class InstructionNode(string nodeID)
|
||||||
{
|
{
|
||||||
public string NodeID { get; init; }
|
public string NodeID { get; init; } = nodeID;
|
||||||
|
|
||||||
public List<string> PreviousNodeIDs { get; } = [];
|
public List<string> PreviousNodeIDs { get; } = [];
|
||||||
|
|
||||||
@@ -154,16 +154,12 @@ public class Day07 : IDay
|
|||||||
|
|
||||||
private InstructionNode GetNode(string nodeID)
|
private InstructionNode GetNode(string nodeID)
|
||||||
{
|
{
|
||||||
InstructionNode node;
|
if (Nodes.TryGetValue(nodeID, out InstructionNode? nodeAux))
|
||||||
if (Nodes.TryGetValue(nodeID, out InstructionNode nodeAux))
|
|
||||||
{
|
{
|
||||||
node = nodeAux;
|
return nodeAux;
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
node = new InstructionNode { NodeID = nodeID, };
|
|
||||||
Nodes.Add(nodeID, node);
|
|
||||||
}
|
}
|
||||||
|
InstructionNode node = new(nodeID);
|
||||||
|
Nodes.Add(nodeID, node);
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -194,7 +190,8 @@ public class Day07 : IDay
|
|||||||
.ToList();
|
.ToList();
|
||||||
if (unusedNodes.Count > 0)
|
if (unusedNodes.Count > 0)
|
||||||
{
|
{
|
||||||
InstructionNode node = unusedNodes.FirstOrDefault();
|
InstructionNode? node = unusedNodes.FirstOrDefault();
|
||||||
|
if (node == null) { continue; }
|
||||||
finalNodes.Add(node);
|
finalNodes.Add(node);
|
||||||
node.Used = true;
|
node.Used = true;
|
||||||
}
|
}
|
||||||
@@ -204,7 +201,7 @@ public class Day07 : IDay
|
|||||||
|
|
||||||
private class SimulatedWorker
|
private class SimulatedWorker
|
||||||
{
|
{
|
||||||
public InstructionNode CurrentInstruction { get; private set; }
|
public InstructionNode? CurrentInstruction { get; private set; }
|
||||||
private int ElapsedTime { get; set; }
|
private int ElapsedTime { get; set; }
|
||||||
|
|
||||||
public void SetInstruction(InstructionNode instruction)
|
public void SetInstruction(InstructionNode instruction)
|
||||||
|
|||||||
@@ -87,16 +87,16 @@ public class Day09 : IDay
|
|||||||
public class Marble
|
public class Marble
|
||||||
{
|
{
|
||||||
public long Value { get; init; }
|
public long Value { get; init; }
|
||||||
public Marble Previous { get; set; }
|
public Marble? Previous { get; set; }
|
||||||
public Marble Next { get; set; }
|
public Marble? Next { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class MarbleGame
|
public class MarbleGame
|
||||||
{
|
{
|
||||||
private Dictionary<long, long> Scores { get; } = new();
|
private Dictionary<long, long> Scores { get; } = new();
|
||||||
|
|
||||||
private Marble _firstMarble;
|
private Marble? _firstMarble;
|
||||||
private Marble _currentMarble;
|
private Marble? _currentMarble;
|
||||||
private long _currentPlayer;
|
private long _currentPlayer;
|
||||||
|
|
||||||
private const long PointValueMultiple = 23;
|
private const long PointValueMultiple = 23;
|
||||||
@@ -119,20 +119,34 @@ public class Day09 : IDay
|
|||||||
Marble newMarble = new() { Value = i + 1 };
|
Marble newMarble = new() { Value = i + 1 };
|
||||||
if ((newMarble.Value % PointValueMultiple) > 0)
|
if ((newMarble.Value % PointValueMultiple) > 0)
|
||||||
{
|
{
|
||||||
Marble previousMarble = _currentMarble.Next;
|
Marble? previousMarble = _currentMarble?.Next;
|
||||||
Marble nextMarble = previousMarble.Next;
|
Marble? nextMarble = previousMarble?.Next;
|
||||||
newMarble.Previous = previousMarble;
|
newMarble.Previous = previousMarble;
|
||||||
newMarble.Next = nextMarble;
|
newMarble.Next = nextMarble;
|
||||||
previousMarble.Next = newMarble;
|
if(previousMarble != null)
|
||||||
nextMarble.Previous = newMarble;
|
{
|
||||||
|
previousMarble.Next = newMarble;
|
||||||
|
}
|
||||||
|
if(nextMarble != null)
|
||||||
|
{
|
||||||
|
nextMarble.Previous = newMarble;
|
||||||
|
}
|
||||||
_currentMarble = newMarble;
|
_currentMarble = newMarble;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Marble marbleToRemove = _currentMarble.Previous.Previous.Previous.Previous.Previous.Previous.Previous;
|
Marble? marbleToRemove = _currentMarble?.Previous?.Previous?.Previous?.Previous?.Previous?.Previous?.Previous;
|
||||||
_currentMarble = marbleToRemove.Next;
|
_currentMarble = marbleToRemove?.Next;
|
||||||
marbleToRemove.Previous.Next = marbleToRemove.Next;
|
if (marbleToRemove == null) { continue; }
|
||||||
marbleToRemove.Next.Previous = marbleToRemove.Previous;
|
|
||||||
|
if(marbleToRemove.Previous != null)
|
||||||
|
{
|
||||||
|
marbleToRemove.Previous.Next = marbleToRemove.Next;
|
||||||
|
}
|
||||||
|
if (marbleToRemove.Next != null)
|
||||||
|
{
|
||||||
|
marbleToRemove.Next.Previous = marbleToRemove.Previous;
|
||||||
|
}
|
||||||
|
|
||||||
long currentPlayerScore = Scores[_currentPlayer] + (newMarble.Value + marbleToRemove.Value);
|
long currentPlayerScore = Scores[_currentPlayer] + (newMarble.Value + marbleToRemove.Value);
|
||||||
Scores[_currentPlayer] = currentPlayerScore;
|
Scores[_currentPlayer] = currentPlayerScore;
|
||||||
@@ -144,14 +158,14 @@ public class Day09 : IDay
|
|||||||
private void PrintStatus()
|
private void PrintStatus()
|
||||||
{
|
{
|
||||||
Console.Write("[{0}] ", _currentPlayer);
|
Console.Write("[{0}] ", _currentPlayer);
|
||||||
Marble marble = _firstMarble;
|
Marble? marble = _firstMarble;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
Console.Write(_currentMarble.Value == marble.Value
|
Console.Write(_currentMarble?.Value == marble?.Value
|
||||||
? "({0}) "
|
? "({0}) "
|
||||||
: "{0} ", marble.Value);
|
: "{0} ", marble?.Value);
|
||||||
marble = marble.Next;
|
marble = marble?.Next;
|
||||||
} while (marble.Value != 0);
|
} while (marble != null && marble.Value != 0);
|
||||||
Console.WriteLine();
|
Console.WriteLine();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -111,8 +111,8 @@ public class Day12 : IDay
|
|||||||
private readonly List<bool> _initialState = [];
|
private readonly List<bool> _initialState = [];
|
||||||
private readonly List<PlantRule> _rules = [];
|
private readonly List<PlantRule> _rules = [];
|
||||||
private long _offsetField;
|
private long _offsetField;
|
||||||
private bool[] _field;
|
private bool[] _field = [];
|
||||||
private bool[] _workField;
|
private bool[] _workField = [];
|
||||||
|
|
||||||
private void Initialize(string[] inputs)
|
private void Initialize(string[] inputs)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -217,7 +217,7 @@ public class Day13 : IDay
|
|||||||
public string ResolvePart1(string[] inputs)
|
public string ResolvePart1(string[] inputs)
|
||||||
{
|
{
|
||||||
Initialize(inputs);
|
Initialize(inputs);
|
||||||
Train collidingTrain;
|
Train? collidingTrain;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
if (ShowProgress) { ShowGrid(); }
|
if (ShowProgress) { ShowGrid(); }
|
||||||
@@ -229,7 +229,7 @@ public class Day13 : IDay
|
|||||||
public string ResolvePart2(string[] inputs)
|
public string ResolvePart2(string[] inputs)
|
||||||
{
|
{
|
||||||
Initialize(inputs);
|
Initialize(inputs);
|
||||||
Train lastCart;
|
Train? lastCart;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
if (ShowProgress) { ShowGrid(); }
|
if (ShowProgress) { ShowGrid(); }
|
||||||
@@ -371,7 +371,7 @@ public class Day13 : IDay
|
|||||||
|
|
||||||
private int _width;
|
private int _width;
|
||||||
private int _height;
|
private int _height;
|
||||||
private char[,] _grid;
|
private char[,] _grid = new char[0, 0];
|
||||||
private readonly List<Train> _trains = [];
|
private readonly List<Train> _trains = [];
|
||||||
|
|
||||||
private void Initialize(string[] inputs)
|
private void Initialize(string[] inputs)
|
||||||
@@ -430,9 +430,9 @@ public class Day13 : IDay
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Train SimulateForFirstCollision()
|
private Train? SimulateForFirstCollision()
|
||||||
{
|
{
|
||||||
Train collidingTrain = null;
|
Train? collidingTrain = null;
|
||||||
IEnumerable<Train> orderedTrains = _trains.OrderBy(t => t.X + (t.Y * _width));
|
IEnumerable<Train> orderedTrains = _trains.OrderBy(t => t.X + (t.Y * _width));
|
||||||
foreach (Train t in orderedTrains)
|
foreach (Train t in orderedTrains)
|
||||||
{
|
{
|
||||||
@@ -443,13 +443,13 @@ public class Day13 : IDay
|
|||||||
return collidingTrain;
|
return collidingTrain;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Train SimulateForLastCart()
|
private Train? SimulateForLastCart()
|
||||||
{
|
{
|
||||||
List<Train> orderedTrains = _trains.OrderBy(t => t.X + (t.Y * _width)).ToList();
|
List<Train> orderedTrains = _trains.OrderBy(t => t.X + (t.Y * _width)).ToList();
|
||||||
foreach (Train t in orderedTrains)
|
foreach (Train t in orderedTrains)
|
||||||
{
|
{
|
||||||
t.Simulate(_grid);
|
t.Simulate(_grid);
|
||||||
Train collidingTrain = _trains.FirstOrDefault(x => x.X == t.X && x.Y == t.Y && t != x);
|
Train? collidingTrain = _trains.FirstOrDefault(x => x.X == t.X && x.Y == t.Y && t != x);
|
||||||
if (collidingTrain != null)
|
if (collidingTrain != null)
|
||||||
{
|
{
|
||||||
_trains.Remove(t);
|
_trains.Remove(t);
|
||||||
@@ -470,7 +470,7 @@ public class Day13 : IDay
|
|||||||
{
|
{
|
||||||
for (int i = 0; i < _width; i++)
|
for (int i = 0; i < _width; i++)
|
||||||
{
|
{
|
||||||
Train t = _trains.FirstOrDefault(x => x.X == i && x.Y == j);
|
Train? t = _trains.FirstOrDefault(x => x.X == i && x.Y == j);
|
||||||
if (t != null)
|
if (t != null)
|
||||||
{
|
{
|
||||||
if (t.Direction == TrainDirection.North)
|
if (t.Direction == TrainDirection.North)
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ public class Day14 : IDay
|
|||||||
{
|
{
|
||||||
private long _numRecipes;
|
private long _numRecipes;
|
||||||
private long _numRecipesAllocated;
|
private long _numRecipesAllocated;
|
||||||
private byte[] _recipes;
|
private byte[] _recipes = [];
|
||||||
private long _idxA;
|
private long _idxA;
|
||||||
private long _idxB;
|
private long _idxB;
|
||||||
private long _searchSkip;
|
private long _searchSkip;
|
||||||
|
|||||||
@@ -444,9 +444,9 @@ public class Day15 : IDay
|
|||||||
|
|
||||||
private int _width;
|
private int _width;
|
||||||
private int _height;
|
private int _height;
|
||||||
private char[,] _map;
|
private char[,] _map = new char[0, 0];
|
||||||
private List<Entity> _entities;
|
private readonly List<Entity> _entities = [];
|
||||||
private BreadthFirstSearchGrid _search;
|
private BreadthFirstSearchGrid? _search;
|
||||||
private int _rounds;
|
private int _rounds;
|
||||||
|
|
||||||
private void Init(string[] inputs)
|
private void Init(string[] inputs)
|
||||||
@@ -454,7 +454,7 @@ public class Day15 : IDay
|
|||||||
_height = inputs.Length;
|
_height = inputs.Length;
|
||||||
_width = inputs.Max(input => input.Length);
|
_width = inputs.Max(input => input.Length);
|
||||||
_map = new char[_width, _height];
|
_map = new char[_width, _height];
|
||||||
_entities = new List<Entity>();
|
_entities.Clear();
|
||||||
for (int j = 0; j < _height; j++)
|
for (int j = 0; j < _height; j++)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < _width; i++)
|
for (int i = 0; i < _width; i++)
|
||||||
@@ -509,7 +509,7 @@ public class Day15 : IDay
|
|||||||
.ThenBy(e => e.X);
|
.ThenBy(e => e.X);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Entity GetBestInRangeTarget(Entity entity, IEnumerable<Entity> targets)
|
private Entity? GetBestInRangeTarget(Entity entity, IEnumerable<Entity> targets)
|
||||||
{
|
{
|
||||||
return targets
|
return targets
|
||||||
.Where(entity.InRangeOf)
|
.Where(entity.InRangeOf)
|
||||||
@@ -521,6 +521,7 @@ public class Day15 : IDay
|
|||||||
|
|
||||||
private void AddTarget(List<Target> targets, int targetX, int targetY, int priority)
|
private void AddTarget(List<Target> targets, int targetX, int targetY, int priority)
|
||||||
{
|
{
|
||||||
|
if (_search == null) { return;}
|
||||||
if (targetX >= 0 && targetX < _width && targetY >= 0 && targetY < _height && _map[targetX, targetY] == '.')
|
if (targetX >= 0 && targetX < _width && targetY >= 0 && targetY < _height && _map[targetX, targetY] == '.')
|
||||||
{
|
{
|
||||||
int distance = _search.QueryDistance(targetX, targetY);
|
int distance = _search.QueryDistance(targetX, targetY);
|
||||||
@@ -538,6 +539,7 @@ public class Day15 : IDay
|
|||||||
|
|
||||||
private void RunBattle()
|
private void RunBattle()
|
||||||
{
|
{
|
||||||
|
if (_search == null) { return;}
|
||||||
_rounds = 0;
|
_rounds = 0;
|
||||||
bool running = true;
|
bool running = true;
|
||||||
do
|
do
|
||||||
@@ -554,7 +556,7 @@ public class Day15 : IDay
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Attack
|
// Attack
|
||||||
Entity targetInRange = GetBestInRangeTarget(entity, entitiesTargets);
|
Entity? targetInRange = GetBestInRangeTarget(entity, entitiesTargets);
|
||||||
if (targetInRange != null)
|
if (targetInRange != null)
|
||||||
{
|
{
|
||||||
entity.Attack(_map, targetInRange);
|
entity.Attack(_map, targetInRange);
|
||||||
@@ -572,7 +574,7 @@ public class Day15 : IDay
|
|||||||
AddTarget(targets, entityTarget.X + 1, entityTarget.Y, priority++);
|
AddTarget(targets, entityTarget.X + 1, entityTarget.Y, priority++);
|
||||||
AddTarget(targets, entityTarget.X, entityTarget.Y + 1, priority++);
|
AddTarget(targets, entityTarget.X, entityTarget.Y + 1, priority++);
|
||||||
}
|
}
|
||||||
Target bestTarget = targets.OrderBy(t => t.Distance).ThenBy(t => t.Priority).FirstOrDefault();
|
Target? bestTarget = targets.OrderBy(t => t.Distance).ThenBy(t => t.Priority).FirstOrDefault();
|
||||||
if (bestTarget != null)
|
if (bestTarget != null)
|
||||||
{
|
{
|
||||||
_search.SearchCharGrid(_map, '.', bestTarget.X, bestTarget.Y);
|
_search.SearchCharGrid(_map, '.', bestTarget.X, bestTarget.Y);
|
||||||
@@ -594,7 +596,7 @@ public class Day15 : IDay
|
|||||||
dirTarget.Distance = _search.QueryDistance(dirTarget.X, dirTarget.Y);
|
dirTarget.Distance = _search.QueryDistance(dirTarget.X, dirTarget.Y);
|
||||||
if (dirTarget.Distance >= 0) targets.Add(dirTarget);
|
if (dirTarget.Distance >= 0) targets.Add(dirTarget);
|
||||||
|
|
||||||
Target finalTarget = targets
|
Target? finalTarget = targets
|
||||||
.OrderBy(t => t.Distance)
|
.OrderBy(t => t.Distance)
|
||||||
.ThenBy(t => t.Priority)
|
.ThenBy(t => t.Priority)
|
||||||
.FirstOrDefault();
|
.FirstOrDefault();
|
||||||
@@ -606,7 +608,7 @@ public class Day15 : IDay
|
|||||||
entity.MoveTo(_map, finalTarget.X, finalTarget.Y);
|
entity.MoveTo(_map, finalTarget.X, finalTarget.Y);
|
||||||
|
|
||||||
// Attack
|
// Attack
|
||||||
Entity targetInRangeAfterMove = GetBestInRangeTarget(entity, entitiesTargets);
|
Entity? targetInRangeAfterMove = GetBestInRangeTarget(entity, entitiesTargets);
|
||||||
if (targetInRangeAfterMove != null)
|
if (targetInRangeAfterMove != null)
|
||||||
{
|
{
|
||||||
entity.Attack(_map, targetInRangeAfterMove);
|
entity.Attack(_map, targetInRangeAfterMove);
|
||||||
@@ -713,7 +715,7 @@ public class Day15 : IDay
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ProcessCell(char[,] grid, char empty, Queue<BFSCell> frontier, BFSCell current, int nextX, int nextY)
|
private void ProcessCell(char[,] grid, char empty, Queue<BFSCell> frontier, BFSCell? current, int nextX, int nextY)
|
||||||
{
|
{
|
||||||
if (nextX < 0 || nextX >= _width || nextY < 0 || nextY >= _height) { return; }
|
if (nextX < 0 || nextX >= _width || nextY < 0 || nextY >= _height) { return; }
|
||||||
if (grid[nextX, nextY] == empty || current == null)
|
if (grid[nextX, nextY] == empty || current == null)
|
||||||
|
|||||||
@@ -87,8 +87,8 @@ public class Day16 : IDay
|
|||||||
int count = 0;
|
int count = 0;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
bool end = false;
|
bool end = false;
|
||||||
int[] beforeRegisters = null;
|
int[]? beforeRegisters = null;
|
||||||
int[] instruction = null;
|
int[]? instruction = null;
|
||||||
const string beforeKeyword = "Before: [";
|
const string beforeKeyword = "Before: [";
|
||||||
const string afterKeyword = "After: [";
|
const string afterKeyword = "After: [";
|
||||||
while (inputs.Length > i)
|
while (inputs.Length > i)
|
||||||
|
|||||||
@@ -68,8 +68,8 @@ public class Day23 : IDay
|
|||||||
public string ResolvePart1(string[] inputs)
|
public string ResolvePart1(string[] inputs)
|
||||||
{
|
{
|
||||||
List<NanoBot> nanoBots = NanoBot.ListFromStrings(inputs);
|
List<NanoBot> nanoBots = NanoBot.ListFromStrings(inputs);
|
||||||
NanoBot bestNanoBot = nanoBots.OrderBy(nanoBot => nanoBot.Range).LastOrDefault();
|
NanoBot? bestNanoBot = nanoBots.OrderBy(nanoBot => nanoBot.Range).LastOrDefault();
|
||||||
int countInRange = nanoBots.Count(nanoBot => bestNanoBot.InRange(nanoBot));
|
int countInRange = nanoBots.Count(nanoBot => bestNanoBot?.InRange(nanoBot) == true);
|
||||||
return countInRange.ToString();
|
return countInRange.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,7 +150,7 @@ public class Day23 : IDay
|
|||||||
public long Z { get; private init; }
|
public long Z { get; private init; }
|
||||||
public long Range { get; private init; }
|
public long Range { get; private init; }
|
||||||
|
|
||||||
private static NanoBot FromString(string strInput)
|
private static NanoBot? FromString(string strInput)
|
||||||
{
|
{
|
||||||
string[] parts = strInput.Split(["pos=<", ",", ">, r="], StringSplitOptions.RemoveEmptyEntries);
|
string[] parts = strInput.Split(["pos=<", ",", ">, r="], StringSplitOptions.RemoveEmptyEntries);
|
||||||
if (parts.Length != 4) { return null; }
|
if (parts.Length != 4) { return null; }
|
||||||
@@ -167,7 +167,7 @@ public class Day23 : IDay
|
|||||||
{
|
{
|
||||||
List<NanoBot> nanoBots = inputs
|
List<NanoBot> nanoBots = inputs
|
||||||
.Select(FromString)
|
.Select(FromString)
|
||||||
.Where(nanoBot => nanoBot != null)
|
.OfType<NanoBot>()
|
||||||
.ToList();
|
.ToList();
|
||||||
return nanoBots;
|
return nanoBots;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,9 +3,11 @@
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
<RootNamespace>AdventOfCode2020.Tests</RootNamespace>
|
<RootNamespace>AdventOfCode2020.Tests</RootNamespace>
|
||||||
|
|
||||||
<IsPackable>false</IsPackable>
|
<IsPackable>false</IsPackable>
|
||||||
|
<IsTestProject>true</IsTestProject>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
<RootNamespace>AdventOfCode2020</RootNamespace>
|
<RootNamespace>AdventOfCode2020</RootNamespace>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ public class Day05 : IDay
|
|||||||
int maxSerialNumber = 0;
|
int maxSerialNumber = 0;
|
||||||
foreach (string input in inputs)
|
foreach (string input in inputs)
|
||||||
{
|
{
|
||||||
Seat seat = Seat_Parse(input);
|
Seat? seat = Seat_Parse(input);
|
||||||
if (seat == null) { continue; }
|
if (seat == null) { continue; }
|
||||||
int newSerialNumber = seat.GetSerialNumber();
|
int newSerialNumber = seat.GetSerialNumber();
|
||||||
if (newSerialNumber > maxSerialNumber) { maxSerialNumber = newSerialNumber; }
|
if (newSerialNumber > maxSerialNumber) { maxSerialNumber = newSerialNumber; }
|
||||||
@@ -83,7 +83,7 @@ public class Day05 : IDay
|
|||||||
}
|
}
|
||||||
foreach (string input in inputs)
|
foreach (string input in inputs)
|
||||||
{
|
{
|
||||||
Seat seat = Seat_Parse(input);
|
Seat? seat = Seat_Parse(input);
|
||||||
if (seat == null) { continue; }
|
if (seat == null) { continue; }
|
||||||
|
|
||||||
seats[seat.Column][seat.Row] = 'X';
|
seats[seat.Column][seat.Row] = 'X';
|
||||||
@@ -166,7 +166,7 @@ public class Day05 : IDay
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Seat Seat_Parse(string input)
|
private Seat? Seat_Parse(string input)
|
||||||
{
|
{
|
||||||
if (input.Length != 10 ||
|
if (input.Length != 10 ||
|
||||||
input.All(c => c == 'F' || c == 'B' || c == 'L' || c == 'R') == false
|
input.All(c => c == 'F' || c == 'B' || c == 'L' || c == 'R') == false
|
||||||
|
|||||||
@@ -123,25 +123,23 @@ public class Day07 : IDay
|
|||||||
|
|
||||||
private class BaggageContainRule
|
private class BaggageContainRule
|
||||||
{
|
{
|
||||||
public string BagColor { get; set; }
|
public string BagColor { get; set; } = string.Empty;
|
||||||
public int Count { get; init; }
|
public int Count { get; init; }
|
||||||
}
|
}
|
||||||
|
|
||||||
private class BaggageRule
|
private class BaggageRule
|
||||||
{
|
{
|
||||||
public string BagColor { get; set; }
|
public string BagColor { get; set; } = string.Empty;
|
||||||
|
|
||||||
public List<BaggageContainRule> Contain { get; init; }
|
public List<BaggageContainRule> Contain { get; } = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
private BaggageRule BaggageRule_Parse(string input)
|
private BaggageRule BaggageRule_Parse(string input)
|
||||||
{
|
{
|
||||||
string[] words = input.Split(' ');
|
string[] words = input.Split(' ');
|
||||||
string status = "Parse Color 1";
|
string status = "Parse Color 1";
|
||||||
BaggageRule rule = new() {
|
BaggageRule rule = new();
|
||||||
Contain = [],
|
BaggageContainRule? containRule = null;
|
||||||
};
|
|
||||||
BaggageContainRule containRule = null;
|
|
||||||
string color1 = string.Empty;
|
string color1 = string.Empty;
|
||||||
|
|
||||||
foreach (string word in words)
|
foreach (string word in words)
|
||||||
@@ -180,6 +178,7 @@ public class Day07 : IDay
|
|||||||
status = "Parse Contain color 2";
|
status = "Parse Contain color 2";
|
||||||
break;
|
break;
|
||||||
case "Parse Contain color 2":
|
case "Parse Contain color 2":
|
||||||
|
if(containRule == null) { break; }
|
||||||
containRule.BagColor = string.Concat(color1, " ", word);
|
containRule.BagColor = string.Concat(color1, " ", word);
|
||||||
rule.Contain.Add(containRule);
|
rule.Contain.Add(containRule);
|
||||||
status = "Parse Contain continue";
|
status = "Parse Contain continue";
|
||||||
|
|||||||
@@ -3,10 +3,11 @@
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<RootNamespace>AdventOfCode2023.Tests</RootNamespace>
|
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
|
<RootNamespace>AdventOfCode2023.Tests</RootNamespace>
|
||||||
|
|
||||||
<IsPackable>false</IsPackable>
|
<IsPackable>false</IsPackable>
|
||||||
|
<IsTestProject>true</IsTestProject>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -4,8 +4,8 @@
|
|||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<RootNamespace>AdventOfCode2023</RootNamespace>
|
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
|
<RootNamespace>AdventOfCode2023</RootNamespace>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -3,8 +3,8 @@
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<RootNamespace>AdventOfCode2024.Tests</RootNamespace>
|
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
|
<RootNamespace>AdventOfCode2024.Tests</RootNamespace>
|
||||||
|
|
||||||
<IsPackable>false</IsPackable>
|
<IsPackable>false</IsPackable>
|
||||||
<IsTestProject>true</IsTestProject>
|
<IsTestProject>true</IsTestProject>
|
||||||
@@ -17,10 +17,6 @@
|
|||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
<PackageReference Include="coverlet.collector" Version="6.0.0">
|
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
|
||||||
<PrivateAssets>all</PrivateAssets>
|
|
||||||
</PackageReference>
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -4,8 +4,8 @@
|
|||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<RootNamespace>AdventOfCode2024</RootNamespace>
|
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
|
<RootNamespace>AdventOfCode2024</RootNamespace>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
Reference in New Issue
Block a user