Apply nullability

This commit is contained in:
2024-12-02 01:37:52 +01:00
parent dafd2526d1
commit e440e7aae1
27 changed files with 125 additions and 101 deletions

View File

@@ -4,6 +4,7 @@
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>AdventOfCode2020</RootNamespace>
</PropertyGroup>

View File

@@ -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

View File

@@ -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<BaggageContainRule> Contain { get; init; }
public List<BaggageContainRule> 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";