Code formatting and warning fixing.

This commit is contained in:
2024-12-01 17:36:23 +01:00
parent 9f8d966b7a
commit dafd2526d1
93 changed files with 739 additions and 823 deletions

View File

@@ -15,7 +15,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AdventOfCode.Common\AdventOfCode.Common.csproj" />
<ProjectReference Include="..\AdventOfCode.Common\AdventOfCode.Common.csproj"/>
</ItemGroup>
</Project>

View File

@@ -78,7 +78,7 @@ public class Day01 : IDay
public string ResolvePart2(string[] inputs)
{
List<(string Text, int Value)> digits = new() {
List<(string Text, int Value)> digits = [
("1", 1),
("2", 2),
("3", 3),
@@ -97,7 +97,7 @@ public class Day01 : IDay
("seven", 7),
("eight", 8),
("nine", 9),
};
];
int sum = 0;
foreach (string line in inputs)

View File

@@ -102,7 +102,7 @@ public class Day03 : IDay
for (column = 0; column < inputs[row].Length; column++)
{
if (inputs[row][column] != '*') { continue; }
List<SchemaNumber> adjacentNumbers = numbers.Where(n => CellAdjacentToSchemaNumber(row, column, n)).ToList();
if (adjacentNumbers.Count != 2) { continue; }
@@ -115,11 +115,11 @@ public class Day03 : IDay
public struct SchemaNumber
{
public string[] Schema { get; set; }
public int Row { get; set; }
public int Column { get; set; }
public int Lenght { get; set; }
public int Value { get; set; }
public string[] Schema { get; init; }
public int Row { get; init; }
public int Column { get; init; }
public int Lenght { get; init; }
public int Value { get; init; }
}
public static SchemaNumber? SearchNextSchemaNumber(string[] schema, int initialRow, int initialColumn)
@@ -176,7 +176,7 @@ public class Day03 : IDay
}
return false;
}
private static bool CellAdjacentToSchemaNumber(int row, int column, SchemaNumber number)
{
int minRow = number.Row - 1;
@@ -188,8 +188,7 @@ public class Day03 : IDay
{
return true;
}
return false;
}
}

View File

@@ -118,7 +118,7 @@ public class Day04 : IDay
_winningNumbers = GetNumbers(strColumns[0]);
_myNumbers = GetNumbers(strColumns[1]);
}
private static List<int> GetNumbers(string strNumbers)
{
string[] parts = strNumbers.Split(' ');
@@ -135,6 +135,5 @@ public class Day04 : IDay
{
return _myNumbers.Count(myNumber => _winningNumbers.Contains(myNumber));
}
}
}

View File

@@ -136,7 +136,7 @@ public class Day05 : IDay
LinesReader reader = new(inputs);
Almanac? almanac = Almanac.Parse(reader);
int i = 0;
List<AlmanacRangeMapping> ranges = new();
List<AlmanacRangeMapping> ranges = [];
while (almanac?.Seeds.Count > i)
{
long seed = almanac.Seeds[i];
@@ -155,21 +155,14 @@ public class Day05 : IDay
}
public class LinesReader
public class LinesReader(string[] lines)
{
private readonly string[] _lines;
private int _index;
public LinesReader(string[] lines)
{
_lines = lines;
_index = 0;
}
public string? GetLine()
{
if (_index >= _lines.Length) { return null; }
string line = _lines[_index];
if (_index >= lines.Length) { return null; }
string line = lines[_index];
_index++;
return line;
}
@@ -298,7 +291,7 @@ public class Day05 : IDay
{
public string Name { get; private init; } = string.Empty;
public List<AlmanacRangeMapping> RangeMappings { get; } = new();
public List<AlmanacRangeMapping> RangeMappings { get; } = [];
public static AlmanacMapping? ParseNext(LinesReader reader)
{
@@ -346,29 +339,32 @@ public class Day05 : IDay
public List<AlmanacRangeMapping> Apply(AlmanacRangeMapping range)
{
List<AlmanacRangeMapping> unMappedRanges = new() {
List<AlmanacRangeMapping> unMappedRanges = [
range,
};
List<AlmanacRangeMapping> newUnMappedRanges = new();
List<AlmanacRangeMapping> mappedRanges = new();
];
List<AlmanacRangeMapping> newUnMappedRanges = [];
List<AlmanacRangeMapping> mappedRanges = [];
int i = 0;
while (RangeMappings.Count > i)
{
AlmanacRangeMapping rangeMapping = RangeMappings[i];
for (int j = 0; j < unMappedRanges.Count; j++)
foreach (AlmanacRangeMapping unMappedRange in unMappedRanges)
{
AlmanacRangeMapping.ClipResult result = rangeMapping.Clip(unMappedRanges[j]);
AlmanacRangeMapping.ClipResult result = rangeMapping.Clip(unMappedRange);
if (result.Clipped != null) { mappedRanges.Add(result.Clipped.Value); }
if (result.PreClip != null) { newUnMappedRanges.Add(result.PreClip.Value); }
if (result.PostClip != null) { newUnMappedRanges.Add(result.PostClip.Value); }
}
unMappedRanges = newUnMappedRanges;
newUnMappedRanges = new List<AlmanacRangeMapping>();
newUnMappedRanges = [];
i++;
}
return mappedRanges.Union(unMappedRanges).ToList();
List<AlmanacRangeMapping> resultMappedRanges = [];
resultMappedRanges.AddRange(mappedRanges);
resultMappedRanges.AddRange(unMappedRanges);
return resultMappedRanges;
}
}
@@ -381,7 +377,7 @@ public class Day05 : IDay
public List<long> Seeds { get; }
private List<AlmanacMapping> Mappings { get; } = new();
private List<AlmanacMapping> Mappings { get; } = [];
public static Almanac? Parse(LinesReader reader)
{

View File

@@ -114,9 +114,9 @@ public class Day07 : IDay
public class CamelCard
{
private readonly static char[] Labels = { 'A', 'K', 'Q', 'J', 'T', '9', '8', '7', '6', '5', '4', '3', '2' };
private readonly static char[] Labels = ['A', 'K', 'Q', 'J', 'T', '9', '8', '7', '6', '5', '4', '3', '2'];
private readonly static char[] LabelsWithJoker = { 'A', 'K', 'Q', 'T', '9', '8', '7', '6', '5', '4', '3', '2', 'J' };
private readonly static char[] LabelsWithJoker = ['A', 'K', 'Q', 'T', '9', '8', '7', '6', '5', '4', '3', '2', 'J'];
private const char JokerLabel = 'J';
public enum Types
@@ -174,7 +174,7 @@ public class Day07 : IDay
dictLabelCounts.Add(JokerLabel, 5);
}
}
if (dictLabelCounts.Count == 1) { return Types.FiveOfAKind; }
if (dictLabelCounts.Count == 5) { return Types.HighCard; }
if (dictLabelCounts.Count == 2)

View File

@@ -105,18 +105,11 @@ public class Day08 : IDay
return steps.ToString();
}
private class MapNode
private class MapNode(string strMapNode)
{
public string Key { get; set; }
public string LeftKey { get; set; }
public string RightKey { get; set; }
public MapNode(string strMapNode)
{
Key = strMapNode.Substring(0, 3);
LeftKey = strMapNode.Substring(7, 3);
RightKey = strMapNode.Substring(12, 3);
}
public string Key { get; } = strMapNode.Substring(0, 3);
public string LeftKey { get; } = strMapNode.Substring(7, 3);
public string RightKey { get; } = strMapNode.Substring(12, 3);
}
private class Map
@@ -155,32 +148,23 @@ public class Day08 : IDay
}
}
private class MapWalker
private class MapWalker(Map map, string startKey)
{
private readonly Map _map;
private MapNode _currentNode;
private MapNode _currentNode = map.GetByKey(startKey) ?? map.Nodes.First();
private long _steps;
public MapWalker(Map map, string startKey)
{
_map = map;
_currentNode = map.GetByKey(startKey) ?? map.Nodes.First();
_steps = 0;
}
public void StepWhile(Func<MapWalker, bool> condition)
{
while (condition(this))
{
char leftRightInstruction = _map.GetInstruction(_steps);
char leftRightInstruction = map.GetInstruction(_steps);
if (leftRightInstruction == 'L')
{
_currentNode = _map.GetByKey(_currentNode.LeftKey) ?? _map.Nodes.First();
_currentNode = map.GetByKey(_currentNode.LeftKey) ?? map.Nodes.First();
}
if (leftRightInstruction == 'R')
{
_currentNode = _map.GetByKey(_currentNode.RightKey) ?? _map.Nodes.First();
_currentNode = map.GetByKey(_currentNode.RightKey) ?? map.Nodes.First();
}
_steps++;
}
@@ -196,7 +180,7 @@ public class Day08 : IDay
get { return _steps; }
}
}
// https://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations
private static long GreatestCommonDivisor(long a, long b)
{

View File

@@ -164,15 +164,15 @@ public class Day09 : IDay
return newLastNumber;
}
public long ExtrapolatePast()
{
_derivatives.Last().Insert(0,0);
_derivatives.Last().Insert(0, 0);
for (int i = _derivatives.Count - 2; i >= 0; i--)
{
long firstDerivative1 = _derivatives[i].First();
long firstDerivative0 = _derivatives[i + 1].First();
_derivatives[i].Insert(0,firstDerivative1 - firstDerivative0);
_derivatives[i].Insert(0, firstDerivative1 - firstDerivative0);
}
long firstNumber = _numbers.First();
long firstDerivative = _derivatives[0].First();

View File

@@ -228,16 +228,10 @@ public class Day10 : IDay
return countInside.ToString();
}
private struct Point
private struct Point(int x, int y)
{
public int X;
public int Y;
public Point(int x, int y)
{
X = x;
Y = y;
}
public int X = x;
public int Y = y;
}
private class PipeMaze
@@ -323,7 +317,7 @@ public class Day10 : IDay
x: point.X + 1,
y: point.Y);
}
if(point.Y == prevPoint.Y && prevPoint.X > point.X)
if (point.Y == prevPoint.Y && prevPoint.X > point.X)
{
return new Point(
x: point.X,
@@ -339,7 +333,7 @@ public class Day10 : IDay
x: point.X - 1,
y: point.Y);
}
if(point.Y == prevPoint.Y && prevPoint.X < point.X)
if (point.Y == prevPoint.Y && prevPoint.X < point.X)
{
return new Point(
x: point.X,
@@ -355,7 +349,7 @@ public class Day10 : IDay
x: point.X - 1,
y: point.Y);
}
if(point.Y == prevPoint.Y && prevPoint.X < point.X)
if (point.Y == prevPoint.Y && prevPoint.X < point.X)
{
return new Point(
x: point.X,
@@ -371,7 +365,7 @@ public class Day10 : IDay
x: point.X + 1,
y: point.Y);
}
if(point.Y == prevPoint.Y && prevPoint.X > point.X)
if (point.Y == prevPoint.Y && prevPoint.X > point.X)
{
return new Point(
x: point.X,
@@ -397,7 +391,7 @@ public class Day10 : IDay
{
Point? nextPoint = FollowPipe(point, prevPoint);
if (nextPoint == null) { break; }
distance++;
int? currentDistance = GetDistance(point);
if (currentDistance > distance || currentDistance == null)
@@ -409,7 +403,7 @@ public class Day10 : IDay
point = nextPoint.Value;
} while (true);
}
public int GetMaxDistance()
{
int max = int.MinValue;
@@ -438,10 +432,10 @@ public class Day10 : IDay
private char CalculateCell(Point point)
{
Point? pointRight =FollowPipe(new Point(point.X + 1, point.Y), point);
Point? pointLeft =FollowPipe(new Point(point.X - 1, point.Y), point);
Point? pointDown =FollowPipe(new Point(point.X, point.Y + 1), point);
Point? pointUp =FollowPipe(new Point(point.X, point.Y - 1), point);
Point? pointRight = FollowPipe(new Point(point.X + 1, point.Y), point);
Point? pointLeft = FollowPipe(new Point(point.X - 1, point.Y), point);
Point? pointDown = FollowPipe(new Point(point.X, point.Y + 1), point);
Point? pointUp = FollowPipe(new Point(point.X, point.Y - 1), point);
if (pointRight != null && pointLeft == null && pointDown != null && pointUp == null)
{
return 'F';
@@ -507,12 +501,12 @@ public class Day10 : IDay
}
else if (cell == 'J')
{
if(cellStart == 'F')
if (cellStart == 'F')
{
inside = inside != true;
cellStart = null;
}
else if(cellStart == 'L')
else if (cellStart == 'L')
{
cellStart = null;
}
@@ -523,23 +517,23 @@ public class Day10 : IDay
{
cellStart = 'F';
}
else if(cellStart == 'J')
else if (cellStart == 'J')
{
inside = inside != true;
cellStart = null;
}
else if(cellStart == '7')
else if (cellStart == '7')
{
cellStart = null;
}
}
else if (cell == '7')
{
if(cellStart == 'F')
if (cellStart == 'F')
{
cellStart = null;
}
else if(cellStart == 'L')
else if (cellStart == 'L')
{
inside = inside != true;
cellStart = null;
@@ -551,11 +545,11 @@ public class Day10 : IDay
{
cellStart = 'L';
}
else if(cellStart == 'J')
else if (cellStart == 'J')
{
cellStart = null;
}
else if(cellStart == '7')
else if (cellStart == '7')
{
inside = inside != true;
cellStart = null;

View File

@@ -130,8 +130,8 @@ public class Day11 : IDay
private struct Point
{
public long X { get; set; }
public long Y { get; set; }
public long X { get; init; }
public long Y { get; init; }
}
private class GalaxyMap

View File

@@ -1,4 +1,3 @@
using System.Text;
namespace AdventOfCode2023;
@@ -212,7 +211,7 @@ public class Day12 : IDay
{
throw new Exception("Invalid input");
}
if (springsRecord.Length < damagedSprings[0])
{
// Invalid match; Not enough data on record for the current damagedSprings group
@@ -230,7 +229,7 @@ public class Day12 : IDay
// Skip current damagedSprings group
return CountPossibleArrangements(springsRecord.Substring(damagedSprings[0]), damagedSprings.Skip(1).ToArray());
}
if (springsRecord.Length < (damagedSprings[0] + 1) || springsRecord[damagedSprings[0]] == '#')
{
// Invalid match; There must be an operational (or unknown) spring between damaged spring groups

View File

@@ -103,24 +103,17 @@ public class Day24 : IDay
throw new NotImplementedException();
}
public struct Vector3D
public struct Vector3D(long x, long y, long z)
{
public readonly long X;
public readonly long Y;
public readonly long Z;
public Vector3D(long x, long y, long z)
{
X = x;
Y = y;
Z = z;
}
public readonly long X = x;
public readonly long Y = y;
public readonly long Z = z;
}
public readonly struct Hail
{
public readonly Vector3D Position;
public readonly Vector3D Velocity;
private readonly Vector3D _velocity;
public Hail(string input)
{
@@ -131,7 +124,7 @@ public class Day24 : IDay
y: Convert.ToInt64(strPosition[1]),
z: Convert.ToInt64(strPosition[2]));
string[] strVelocity = parts[1].Split(", ");
Velocity = new Vector3D(
_velocity = new Vector3D(
x: Convert.ToInt64(strVelocity[0]),
y: Convert.ToInt64(strVelocity[1]),
z: Convert.ToInt64(strVelocity[2]));
@@ -139,21 +132,21 @@ public class Day24 : IDay
public (bool intersects, double s, double t, Vector3D point) Intersect2D(Hail other)
{
long s_Div = (-other.Velocity.X * Velocity.Y + Velocity.X * other.Velocity.Y);
long s_Div = (-other._velocity.X * _velocity.Y + _velocity.X * other._velocity.Y);
if (s_Div == 0)
{
return (false, 0, 0, new Vector3D());
}
double s = (-Velocity.Y * (Position.X - other.Position.X) + Velocity.X * (Position.Y - other.Position.Y)) / (double)s_Div;
double s = (-_velocity.Y * (Position.X - other.Position.X) + _velocity.X * (Position.Y - other.Position.Y)) / (double)s_Div;
long t_Div = (-other.Velocity.X * Velocity.Y + Velocity.X * other.Velocity.Y);
long t_Div = (-other._velocity.X * _velocity.Y + _velocity.X * other._velocity.Y);
if (t_Div == 0)
{
return (false, 0, 0, new Vector3D());
}
double t = (other.Velocity.X * (Position.Y - other.Position.Y) - other.Velocity.Y * (Position.X - other.Position.X)) / (double)t_Div;
double t = (other._velocity.X * (Position.Y - other.Position.Y) - other._velocity.Y * (Position.X - other.Position.X)) / (double)t_Div;
Vector3D intersection = new((long)(Position.X + t * Velocity.X), (long)(Position.Y + t * Velocity.Y), 0);
Vector3D intersection = new((long)(Position.X + t * _velocity.X), (long)(Position.Y + t * _velocity.Y), 0);
return (true, s, t, intersection);
}
}