From e440e7aae1fd2a14fff9c158325d24c9bc90cd6d Mon Sep 17 00:00:00 2001 From: "Valeriano A.R." Date: Mon, 2 Dec 2024 01:37:52 +0100 Subject: [PATCH] Apply nullability --- .../AdventOfCode.Common.csproj | 2 +- .../AdventOfCode2017.Tests.csproj | 2 + AdventOfCode2017/AdventOfCode2017.csproj | 1 + AdventOfCode2017/Day03.cs | 4 +- .../AdventOfCode2018.Tests.csproj | 2 + AdventOfCode2018.Tests/Day06_Tests.cs | 32 ++++++++----- AdventOfCode2018/AdventOfCode2018.csproj | 1 + AdventOfCode2018/Day01.cs | 4 +- AdventOfCode2018/Day03.cs | 4 +- AdventOfCode2018/Day04.cs | 11 ++--- AdventOfCode2018/Day06.cs | 3 +- AdventOfCode2018/Day07.cs | 21 ++++---- AdventOfCode2018/Day09.cs | 48 ++++++++++++------- AdventOfCode2018/Day12.cs | 4 +- AdventOfCode2018/Day13.cs | 16 +++---- AdventOfCode2018/Day14.cs | 2 +- AdventOfCode2018/Day15.cs | 22 +++++---- AdventOfCode2018/Day16.cs | 4 +- AdventOfCode2018/Day23.cs | 8 ++-- .../AdventOfCode2020.Tests.csproj | 2 + AdventOfCode2020/AdventOfCode2020.csproj | 1 + AdventOfCode2020/Day05.cs | 6 +-- AdventOfCode2020/Day07.cs | 13 +++-- .../AdventOfCode2023.Tests.csproj | 3 +- AdventOfCode2023/AdventOfCode2023.csproj | 2 +- .../AdventOfCode2024.Tests.csproj | 6 +-- AdventOfCode2024/AdventOfCode2024.csproj | 2 +- 27 files changed, 125 insertions(+), 101 deletions(-) diff --git a/AdventOfCode.Common/AdventOfCode.Common.csproj b/AdventOfCode.Common/AdventOfCode.Common.csproj index 742dce4..31e67fa 100644 --- a/AdventOfCode.Common/AdventOfCode.Common.csproj +++ b/AdventOfCode.Common/AdventOfCode.Common.csproj @@ -3,8 +3,8 @@ net8.0 enable - AdventOfCode.Common enable + AdventOfCode.Common diff --git a/AdventOfCode2017.Tests/AdventOfCode2017.Tests.csproj b/AdventOfCode2017.Tests/AdventOfCode2017.Tests.csproj index 3c7d317..be33a54 100644 --- a/AdventOfCode2017.Tests/AdventOfCode2017.Tests.csproj +++ b/AdventOfCode2017.Tests/AdventOfCode2017.Tests.csproj @@ -3,9 +3,11 @@ net8.0 enable + enable AdventOfCode2017.Tests false + true diff --git a/AdventOfCode2017/AdventOfCode2017.csproj b/AdventOfCode2017/AdventOfCode2017.csproj index a9ecb47..272cef0 100644 --- a/AdventOfCode2017/AdventOfCode2017.csproj +++ b/AdventOfCode2017/AdventOfCode2017.csproj @@ -4,6 +4,7 @@ Exe net8.0 enable + enable AdventOfCode2017 diff --git a/AdventOfCode2017/Day03.cs b/AdventOfCode2017/Day03.cs index ae02610..9680899 100644 --- a/AdventOfCode2017/Day03.cs +++ b/AdventOfCode2017/Day03.cs @@ -30,11 +30,11 @@ public class Day03 : IDay { public string ResolvePart1(string[] inputs) { - return null; + return string.Empty; } public string ResolvePart2(string[] inputs) { - return null; + return string.Empty; } } \ No newline at end of file diff --git a/AdventOfCode2018.Tests/AdventOfCode2018.Tests.csproj b/AdventOfCode2018.Tests/AdventOfCode2018.Tests.csproj index 6d9a17c..3b7a6db 100644 --- a/AdventOfCode2018.Tests/AdventOfCode2018.Tests.csproj +++ b/AdventOfCode2018.Tests/AdventOfCode2018.Tests.csproj @@ -3,9 +3,11 @@ net8.0 enable + enable AdventOfCode2018.Tests false + true diff --git a/AdventOfCode2018.Tests/Day06_Tests.cs b/AdventOfCode2018.Tests/Day06_Tests.cs index 28720a3..7123a2d 100644 --- a/AdventOfCode2018.Tests/Day06_Tests.cs +++ b/AdventOfCode2018.Tests/Day06_Tests.cs @@ -7,28 +7,28 @@ public class Day06_Tests [Fact] 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.Y); + Assert.Equal(1, point?.X); + Assert.Equal(1, point?.Y); } [Fact] 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(6, point.Y); + Assert.Equal(1, point?.X); + Assert.Equal(6, point?.Y); } [Fact] 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(9, point.Y); + Assert.Equal(8, point?.X); + Assert.Equal(9, point?.Y); } #endregion ChronoPoint_FromString @@ -38,9 +38,12 @@ public class Day06_Tests [Fact] public void ChronoPoint_ManhattanDistance__Test1() { - Day06.ChronoPoint p0 = Day06.ChronoPoint.FromString("8, 9"); - Day06.ChronoPoint p1 = Day06.ChronoPoint.FromString("1, 6"); + Day06.ChronoPoint? p0 = Day06.ChronoPoint.FromString("8, 9"); + Day06.ChronoPoint? p1 = Day06.ChronoPoint.FromString("1, 6"); + Assert.NotNull(p0); + Assert.NotNull(p1); + int distance = Day06.ChronoPoint.ManhattanDistance(p0, p1); Assert.Equal(10, distance); @@ -49,9 +52,12 @@ public class Day06_Tests [Fact] public void ChronoPoint_ManhattanDistance__Test2() { - Day06.ChronoPoint p0 = Day06.ChronoPoint.FromString("1, 1"); - Day06.ChronoPoint p1 = Day06.ChronoPoint.FromString("1, 6"); + Day06.ChronoPoint? p0 = Day06.ChronoPoint.FromString("1, 1"); + Day06.ChronoPoint? p1 = Day06.ChronoPoint.FromString("1, 6"); + Assert.NotNull(p0); + Assert.NotNull(p1); + int distance = Day06.ChronoPoint.ManhattanDistance(p0, p1); Assert.Equal(5, distance); diff --git a/AdventOfCode2018/AdventOfCode2018.csproj b/AdventOfCode2018/AdventOfCode2018.csproj index 9a95a67..13319b9 100644 --- a/AdventOfCode2018/AdventOfCode2018.csproj +++ b/AdventOfCode2018/AdventOfCode2018.csproj @@ -4,6 +4,7 @@ Exe net8.0 enable + enable AdventOfCode2018 diff --git a/AdventOfCode2018/Day01.cs b/AdventOfCode2018/Day01.cs index 6061a6d..126f01f 100644 --- a/AdventOfCode2018/Day01.cs +++ b/AdventOfCode2018/Day01.cs @@ -82,7 +82,7 @@ public class Day01 : IDay public string ResolvePart2(string[] inputs) { int accumulator = 0; - List accumulatorHistory = new(); + List accumulatorHistory = []; int? repeatedAccumulator = null; while (repeatedAccumulator == null) { @@ -108,6 +108,6 @@ public class Day01 : IDay } } } - return repeatedAccumulator.ToString(); + return repeatedAccumulator.ToString() ?? string.Empty; } } \ No newline at end of file diff --git a/AdventOfCode2018/Day03.cs b/AdventOfCode2018/Day03.cs index bd6d7dd..ea871bb 100644 --- a/AdventOfCode2018/Day03.cs +++ b/AdventOfCode2018/Day03.cs @@ -94,7 +94,7 @@ public class Day03 : IDay { List claims = inputs.Select(Claim.FromString).ToList(); - Claim unOverlappingClaim = null; + Claim? unOverlappingClaim = null; for (int i = 0; i < claims.Count; i++) { bool overlaps = false; @@ -113,7 +113,7 @@ public class Day03 : IDay break; } } - return unOverlappingClaim.ID.ToString(); + return unOverlappingClaim?.ID.ToString() ?? string.Empty; } diff --git a/AdventOfCode2018/Day04.cs b/AdventOfCode2018/Day04.cs index b9b9bef..43848f1 100644 --- a/AdventOfCode2018/Day04.cs +++ b/AdventOfCode2018/Day04.cs @@ -71,7 +71,7 @@ public class Day04 : IDay Dictionary dictFullHistogram = BuildFullHistogram(guardEvents); // Find sleepier guard - GuardSleepHistogram highestSleeperHistogram = null; + GuardSleepHistogram? highestSleeperHistogram = null; long highestTotalSleep = long.MinValue; foreach (GuardSleepHistogram guardHistogram in dictFullHistogram.Values) { @@ -83,6 +83,7 @@ public class Day04 : IDay highestTotalSleep = totalSleep; } } + if (highestSleeperHistogram == null) { return string.Empty; } // Find sleepier minute int maxSleepMinute = int.MinValue; @@ -156,14 +157,12 @@ public class Day04 : IDay 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); + continue; } - else - { - dictFullHistogram.Add(dayGuardHistogram.ID, dayGuardHistogram); - } + dictFullHistogram.Add(dayGuardHistogram.ID, dayGuardHistogram); } } diff --git a/AdventOfCode2018/Day06.cs b/AdventOfCode2018/Day06.cs index 29dec71..d140bdc 100644 --- a/AdventOfCode2018/Day06.cs +++ b/AdventOfCode2018/Day06.cs @@ -97,6 +97,7 @@ public class Day06 : IDay return inputs .Where(input => string.IsNullOrEmpty(input) == false) .Select(ChronoPoint.FromString) + .OfType() .ToList(); } @@ -197,7 +198,7 @@ public class Day06 : IDay public int X { 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; } string[] parts = strPoint.Split([", "], StringSplitOptions.RemoveEmptyEntries); diff --git a/AdventOfCode2018/Day07.cs b/AdventOfCode2018/Day07.cs index e00231a..d0305e7 100644 --- a/AdventOfCode2018/Day07.cs +++ b/AdventOfCode2018/Day07.cs @@ -128,9 +128,9 @@ public class Day07 : IDay return totalElapsedTime.ToString(); } - public class InstructionNode + public class InstructionNode(string nodeID) { - public string NodeID { get; init; } + public string NodeID { get; init; } = nodeID; public List PreviousNodeIDs { get; } = []; @@ -154,16 +154,12 @@ public class Day07 : IDay private InstructionNode GetNode(string nodeID) { - InstructionNode node; - if (Nodes.TryGetValue(nodeID, out InstructionNode nodeAux)) + if (Nodes.TryGetValue(nodeID, out InstructionNode? nodeAux)) { - node = nodeAux; - } - else - { - node = new InstructionNode { NodeID = nodeID, }; - Nodes.Add(nodeID, node); + return nodeAux; } + InstructionNode node = new(nodeID); + Nodes.Add(nodeID, node); return node; } @@ -194,7 +190,8 @@ public class Day07 : IDay .ToList(); if (unusedNodes.Count > 0) { - InstructionNode node = unusedNodes.FirstOrDefault(); + InstructionNode? node = unusedNodes.FirstOrDefault(); + if (node == null) { continue; } finalNodes.Add(node); node.Used = true; } @@ -204,7 +201,7 @@ public class Day07 : IDay private class SimulatedWorker { - public InstructionNode CurrentInstruction { get; private set; } + public InstructionNode? CurrentInstruction { get; private set; } private int ElapsedTime { get; set; } public void SetInstruction(InstructionNode instruction) diff --git a/AdventOfCode2018/Day09.cs b/AdventOfCode2018/Day09.cs index 8e077cb..8d8de87 100644 --- a/AdventOfCode2018/Day09.cs +++ b/AdventOfCode2018/Day09.cs @@ -87,16 +87,16 @@ public class Day09 : IDay public class Marble { public long Value { get; init; } - public Marble Previous { get; set; } - public Marble Next { get; set; } + public Marble? Previous { get; set; } + public Marble? Next { get; set; } } public class MarbleGame { private Dictionary Scores { get; } = new(); - private Marble _firstMarble; - private Marble _currentMarble; + private Marble? _firstMarble; + private Marble? _currentMarble; private long _currentPlayer; private const long PointValueMultiple = 23; @@ -119,20 +119,34 @@ public class Day09 : IDay Marble newMarble = new() { Value = i + 1 }; if ((newMarble.Value % PointValueMultiple) > 0) { - Marble previousMarble = _currentMarble.Next; - Marble nextMarble = previousMarble.Next; + Marble? previousMarble = _currentMarble?.Next; + Marble? nextMarble = previousMarble?.Next; newMarble.Previous = previousMarble; newMarble.Next = nextMarble; - previousMarble.Next = newMarble; - nextMarble.Previous = newMarble; + if(previousMarble != null) + { + previousMarble.Next = newMarble; + } + if(nextMarble != null) + { + nextMarble.Previous = newMarble; + } _currentMarble = newMarble; } else { - Marble marbleToRemove = _currentMarble.Previous.Previous.Previous.Previous.Previous.Previous.Previous; - _currentMarble = marbleToRemove.Next; - marbleToRemove.Previous.Next = marbleToRemove.Next; - marbleToRemove.Next.Previous = marbleToRemove.Previous; + Marble? marbleToRemove = _currentMarble?.Previous?.Previous?.Previous?.Previous?.Previous?.Previous?.Previous; + _currentMarble = marbleToRemove?.Next; + if (marbleToRemove == null) { continue; } + + 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); Scores[_currentPlayer] = currentPlayerScore; @@ -144,14 +158,14 @@ public class Day09 : IDay private void PrintStatus() { Console.Write("[{0}] ", _currentPlayer); - Marble marble = _firstMarble; + Marble? marble = _firstMarble; do { - Console.Write(_currentMarble.Value == marble.Value + Console.Write(_currentMarble?.Value == marble?.Value ? "({0}) " - : "{0} ", marble.Value); - marble = marble.Next; - } while (marble.Value != 0); + : "{0} ", marble?.Value); + marble = marble?.Next; + } while (marble != null && marble.Value != 0); Console.WriteLine(); } diff --git a/AdventOfCode2018/Day12.cs b/AdventOfCode2018/Day12.cs index a57f2bd..94f2c2b 100644 --- a/AdventOfCode2018/Day12.cs +++ b/AdventOfCode2018/Day12.cs @@ -111,8 +111,8 @@ public class Day12 : IDay private readonly List _initialState = []; private readonly List _rules = []; private long _offsetField; - private bool[] _field; - private bool[] _workField; + private bool[] _field = []; + private bool[] _workField = []; private void Initialize(string[] inputs) { diff --git a/AdventOfCode2018/Day13.cs b/AdventOfCode2018/Day13.cs index d1a07fe..0f9f798 100644 --- a/AdventOfCode2018/Day13.cs +++ b/AdventOfCode2018/Day13.cs @@ -217,7 +217,7 @@ public class Day13 : IDay public string ResolvePart1(string[] inputs) { Initialize(inputs); - Train collidingTrain; + Train? collidingTrain; do { if (ShowProgress) { ShowGrid(); } @@ -229,7 +229,7 @@ public class Day13 : IDay public string ResolvePart2(string[] inputs) { Initialize(inputs); - Train lastCart; + Train? lastCart; do { if (ShowProgress) { ShowGrid(); } @@ -371,7 +371,7 @@ public class Day13 : IDay private int _width; private int _height; - private char[,] _grid; + private char[,] _grid = new char[0, 0]; private readonly List _trains = []; 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 orderedTrains = _trains.OrderBy(t => t.X + (t.Y * _width)); foreach (Train t in orderedTrains) { @@ -443,13 +443,13 @@ public class Day13 : IDay return collidingTrain; } - private Train SimulateForLastCart() + private Train? SimulateForLastCart() { List orderedTrains = _trains.OrderBy(t => t.X + (t.Y * _width)).ToList(); foreach (Train t in orderedTrains) { 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) { _trains.Remove(t); @@ -470,7 +470,7 @@ public class Day13 : IDay { 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.Direction == TrainDirection.North) diff --git a/AdventOfCode2018/Day14.cs b/AdventOfCode2018/Day14.cs index 5957905..9d2cfa6 100644 --- a/AdventOfCode2018/Day14.cs +++ b/AdventOfCode2018/Day14.cs @@ -62,7 +62,7 @@ public class Day14 : IDay { private long _numRecipes; private long _numRecipesAllocated; - private byte[] _recipes; + private byte[] _recipes = []; private long _idxA; private long _idxB; private long _searchSkip; diff --git a/AdventOfCode2018/Day15.cs b/AdventOfCode2018/Day15.cs index 394969d..d17a696 100644 --- a/AdventOfCode2018/Day15.cs +++ b/AdventOfCode2018/Day15.cs @@ -444,9 +444,9 @@ public class Day15 : IDay private int _width; private int _height; - private char[,] _map; - private List _entities; - private BreadthFirstSearchGrid _search; + private char[,] _map = new char[0, 0]; + private readonly List _entities = []; + private BreadthFirstSearchGrid? _search; private int _rounds; private void Init(string[] inputs) @@ -454,7 +454,7 @@ public class Day15 : IDay _height = inputs.Length; _width = inputs.Max(input => input.Length); _map = new char[_width, _height]; - _entities = new List(); + _entities.Clear(); for (int j = 0; j < _height; j++) { for (int i = 0; i < _width; i++) @@ -509,7 +509,7 @@ public class Day15 : IDay .ThenBy(e => e.X); } - private Entity GetBestInRangeTarget(Entity entity, IEnumerable targets) + private Entity? GetBestInRangeTarget(Entity entity, IEnumerable targets) { return targets .Where(entity.InRangeOf) @@ -521,6 +521,7 @@ public class Day15 : IDay private void AddTarget(List targets, int targetX, int targetY, int priority) { + if (_search == null) { return;} if (targetX >= 0 && targetX < _width && targetY >= 0 && targetY < _height && _map[targetX, targetY] == '.') { int distance = _search.QueryDistance(targetX, targetY); @@ -538,6 +539,7 @@ public class Day15 : IDay private void RunBattle() { + if (_search == null) { return;} _rounds = 0; bool running = true; do @@ -554,7 +556,7 @@ public class Day15 : IDay } // Attack - Entity targetInRange = GetBestInRangeTarget(entity, entitiesTargets); + Entity? targetInRange = GetBestInRangeTarget(entity, entitiesTargets); if (targetInRange != null) { entity.Attack(_map, targetInRange); @@ -572,7 +574,7 @@ public class Day15 : IDay AddTarget(targets, entityTarget.X + 1, entityTarget.Y, 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) { _search.SearchCharGrid(_map, '.', bestTarget.X, bestTarget.Y); @@ -594,7 +596,7 @@ public class Day15 : IDay dirTarget.Distance = _search.QueryDistance(dirTarget.X, dirTarget.Y); if (dirTarget.Distance >= 0) targets.Add(dirTarget); - Target finalTarget = targets + Target? finalTarget = targets .OrderBy(t => t.Distance) .ThenBy(t => t.Priority) .FirstOrDefault(); @@ -606,7 +608,7 @@ public class Day15 : IDay entity.MoveTo(_map, finalTarget.X, finalTarget.Y); // Attack - Entity targetInRangeAfterMove = GetBestInRangeTarget(entity, entitiesTargets); + Entity? targetInRangeAfterMove = GetBestInRangeTarget(entity, entitiesTargets); if (targetInRangeAfterMove != null) { entity.Attack(_map, targetInRangeAfterMove); @@ -713,7 +715,7 @@ public class Day15 : IDay } } - private void ProcessCell(char[,] grid, char empty, Queue frontier, BFSCell current, int nextX, int nextY) + private void ProcessCell(char[,] grid, char empty, Queue frontier, BFSCell? current, int nextX, int nextY) { if (nextX < 0 || nextX >= _width || nextY < 0 || nextY >= _height) { return; } if (grid[nextX, nextY] == empty || current == null) diff --git a/AdventOfCode2018/Day16.cs b/AdventOfCode2018/Day16.cs index 2c40861..97423b7 100644 --- a/AdventOfCode2018/Day16.cs +++ b/AdventOfCode2018/Day16.cs @@ -87,8 +87,8 @@ public class Day16 : IDay int count = 0; int i = 0; bool end = false; - int[] beforeRegisters = null; - int[] instruction = null; + int[]? beforeRegisters = null; + int[]? instruction = null; const string beforeKeyword = "Before: ["; const string afterKeyword = "After: ["; while (inputs.Length > i) diff --git a/AdventOfCode2018/Day23.cs b/AdventOfCode2018/Day23.cs index ef92cd1..4988ca6 100644 --- a/AdventOfCode2018/Day23.cs +++ b/AdventOfCode2018/Day23.cs @@ -68,8 +68,8 @@ public class Day23 : IDay public string ResolvePart1(string[] inputs) { List nanoBots = NanoBot.ListFromStrings(inputs); - NanoBot bestNanoBot = nanoBots.OrderBy(nanoBot => nanoBot.Range).LastOrDefault(); - int countInRange = nanoBots.Count(nanoBot => bestNanoBot.InRange(nanoBot)); + NanoBot? bestNanoBot = nanoBots.OrderBy(nanoBot => nanoBot.Range).LastOrDefault(); + int countInRange = nanoBots.Count(nanoBot => bestNanoBot?.InRange(nanoBot) == true); return countInRange.ToString(); } @@ -150,7 +150,7 @@ public class Day23 : IDay public long Z { 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); if (parts.Length != 4) { return null; } @@ -167,7 +167,7 @@ public class Day23 : IDay { List nanoBots = inputs .Select(FromString) - .Where(nanoBot => nanoBot != null) + .OfType() .ToList(); return nanoBots; } diff --git a/AdventOfCode2020.Tests/AdventOfCode2020.Tests.csproj b/AdventOfCode2020.Tests/AdventOfCode2020.Tests.csproj index cb63640..c99e3d7 100644 --- a/AdventOfCode2020.Tests/AdventOfCode2020.Tests.csproj +++ b/AdventOfCode2020.Tests/AdventOfCode2020.Tests.csproj @@ -3,9 +3,11 @@ net8.0 enable + enable AdventOfCode2020.Tests false + true diff --git a/AdventOfCode2020/AdventOfCode2020.csproj b/AdventOfCode2020/AdventOfCode2020.csproj index 40ec99a..9461cbc 100644 --- a/AdventOfCode2020/AdventOfCode2020.csproj +++ b/AdventOfCode2020/AdventOfCode2020.csproj @@ -4,6 +4,7 @@ Exe net8.0 enable + enable AdventOfCode2020 diff --git a/AdventOfCode2020/Day05.cs b/AdventOfCode2020/Day05.cs index 544d859..c67a895 100644 --- a/AdventOfCode2020/Day05.cs +++ b/AdventOfCode2020/Day05.cs @@ -64,7 +64,7 @@ public class Day05 : IDay int maxSerialNumber = 0; foreach (string input in inputs) { - Seat seat = Seat_Parse(input); + Seat? seat = Seat_Parse(input); if (seat == null) { continue; } int newSerialNumber = seat.GetSerialNumber(); if (newSerialNumber > maxSerialNumber) { maxSerialNumber = newSerialNumber; } @@ -83,7 +83,7 @@ public class Day05 : IDay } foreach (string input in inputs) { - Seat seat = Seat_Parse(input); + Seat? seat = Seat_Parse(input); if (seat == null) { continue; } 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 || input.All(c => c == 'F' || c == 'B' || c == 'L' || c == 'R') == false diff --git a/AdventOfCode2020/Day07.cs b/AdventOfCode2020/Day07.cs index 5309cf3..a69802c 100644 --- a/AdventOfCode2020/Day07.cs +++ b/AdventOfCode2020/Day07.cs @@ -123,25 +123,23 @@ public class Day07 : IDay private class BaggageContainRule { - public string BagColor { get; set; } + public string BagColor { get; set; } = string.Empty; public int Count { get; init; } } private class BaggageRule { - public string BagColor { get; set; } + public string BagColor { get; set; } = string.Empty; - public List Contain { get; init; } + public List Contain { get; } = []; } private BaggageRule BaggageRule_Parse(string input) { string[] words = input.Split(' '); string status = "Parse Color 1"; - BaggageRule rule = new() { - Contain = [], - }; - BaggageContainRule containRule = null; + BaggageRule rule = new(); + BaggageContainRule? containRule = null; string color1 = string.Empty; foreach (string word in words) @@ -180,6 +178,7 @@ public class Day07 : IDay status = "Parse Contain color 2"; break; case "Parse Contain color 2": + if(containRule == null) { break; } containRule.BagColor = string.Concat(color1, " ", word); rule.Contain.Add(containRule); status = "Parse Contain continue"; diff --git a/AdventOfCode2023.Tests/AdventOfCode2023.Tests.csproj b/AdventOfCode2023.Tests/AdventOfCode2023.Tests.csproj index 916f355..478c6be 100644 --- a/AdventOfCode2023.Tests/AdventOfCode2023.Tests.csproj +++ b/AdventOfCode2023.Tests/AdventOfCode2023.Tests.csproj @@ -3,10 +3,11 @@ net8.0 enable - AdventOfCode2023.Tests enable + AdventOfCode2023.Tests false + true diff --git a/AdventOfCode2023/AdventOfCode2023.csproj b/AdventOfCode2023/AdventOfCode2023.csproj index 099f4ac..e2244a7 100644 --- a/AdventOfCode2023/AdventOfCode2023.csproj +++ b/AdventOfCode2023/AdventOfCode2023.csproj @@ -4,8 +4,8 @@ Exe net8.0 enable - AdventOfCode2023 enable + AdventOfCode2023 diff --git a/AdventOfCode2024.Tests/AdventOfCode2024.Tests.csproj b/AdventOfCode2024.Tests/AdventOfCode2024.Tests.csproj index 3497b52..ef5f3ae 100644 --- a/AdventOfCode2024.Tests/AdventOfCode2024.Tests.csproj +++ b/AdventOfCode2024.Tests/AdventOfCode2024.Tests.csproj @@ -3,8 +3,8 @@ net8.0 enable - AdventOfCode2024.Tests enable + AdventOfCode2024.Tests false true @@ -17,10 +17,6 @@ runtime; build; native; contentfiles; analyzers; buildtransitive all - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - diff --git a/AdventOfCode2024/AdventOfCode2024.csproj b/AdventOfCode2024/AdventOfCode2024.csproj index f097193..eb2cab6 100644 --- a/AdventOfCode2024/AdventOfCode2024.csproj +++ b/AdventOfCode2024/AdventOfCode2024.csproj @@ -4,8 +4,8 @@ Exe net8.0 enable - AdventOfCode2024 enable + AdventOfCode2024