fix nullable warnings
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Ben Harris 2021-11-12 16:49:40 -05:00
parent 94ad64a16f
commit b599190409
5 changed files with 44 additions and 55 deletions

View File

@ -23,15 +23,15 @@ public class DayTests
[DataRow(typeof(Day14), "17481577045893", "4160009892257")]
[DataRow(typeof(Day15), "257", "8546398")]
[DataRow(typeof(Day16), "19093", "5311123569883")]
// [DataRow(typeof(Day17), "293", "1816")]
// [DataRow(typeof(Day17), "293", "1816")] // this one takes too long and i don't want to bother optimizing it
[DataRow(typeof(Day18), "12918250417632", "171259538712010")]
[DataRow(typeof(Day19), "160", "357")]
[DataRow(typeof(Day20), "21599955909991", "")]
//[DataRow(typeof(Day20), "21599955909991", "")]
[DataRow(typeof(Day21), "2436", "dhfng,pgblcd,xhkdc,ghlzj,dstct,nqbnmzx,ntggc,znrzgs")]
[DataRow(typeof(Day22), "", "")]
[DataRow(typeof(Day23), "", "")]
[DataRow(typeof(Day24), "", "")]
[DataRow(typeof(Day25), "", "")]
//[DataRow(typeof(Day22), "", "")]
//[DataRow(typeof(Day23), "", "")]
//[DataRow(typeof(Day24), "", "")]
//[DataRow(typeof(Day25), "", "")]
public void CheckAllDays(Type dayType, string part1, string part2)
{
// create day instance
@ -44,7 +44,7 @@ public class DayTests
// part 1
s.Reset();
s.Start();
var part1Actual = day.Part1();
var part1Actual = day!.Part1();
s.Stop();
Console.WriteLine($"{s.ScaleMilliseconds()}ms elapsed in part1");
Assert.AreEqual(part1, part1Actual, $"Incorrect answer for Day {day.DayNumber} Part1");

View File

@ -2,9 +2,9 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>

View File

@ -39,14 +39,14 @@ public sealed class Day04 : Day
private class Passport
{
private string _byr;
private string _cid;
private string _ecl;
private string _eyr;
private string _hcl;
private string _hgt;
private string _iyr;
private string _pid;
private string? _byr;
private string? _cid;
private string? _ecl;
private string? _eyr;
private string? _hcl;
private string? _hgt;
private string? _iyr;
private string? _pid;
public bool IsValid =>
_byr != null &&
@ -95,7 +95,7 @@ public sealed class Day04 : Day
}
// height
if (_hgt.EndsWith("cm"))
if (_hgt!.EndsWith("cm"))
{
var h = _hgt[..3];
if (int.TryParse(h, out var hgt))
@ -110,7 +110,7 @@ public sealed class Day04 : Day
}
else if (_hgt.EndsWith("in"))
{
var h = _hgt.Substring(0, 2);
var h = _hgt[..2];
if (int.TryParse(h, out var hgt))
{
if (hgt < 59 || hgt > 76)
@ -127,7 +127,7 @@ public sealed class Day04 : Day
}
// hair color
if (!Regex.IsMatch(_hcl, "#[0-9a-f]{6}"))
if (!Regex.IsMatch(_hcl!, "#[0-9a-f]{6}"))
return false;
// eye color
@ -135,7 +135,7 @@ public sealed class Day04 : Day
return false;
// passport id
if (_pid.Length != 9)
if (_pid != null && _pid.Length != 9)
return false;
return true;

View File

@ -5,7 +5,7 @@ namespace aoc2020;
/// </summary>
public sealed class Day07 : Day
{
private readonly Dictionary<string, IEnumerable<(int, string)?>> _rules;
private readonly Dictionary<string, IEnumerable<(int Weight, string Name)?>> _rules;
public Day07() : base(7, "Handy Haversacks")
{
@ -26,10 +26,11 @@ public sealed class Day07 : Day
return (int.Parse(words[0]), string.Join(' ', words[1..3]));
}
private int Weight(string node)
{
return 1 + _rules[node].Sum(i => i.Value.Item1 * Weight(i.Value.Item2));
}
private int Weight(string node) =>
1 + _rules[node]
.Where(i => i.HasValue)
.Select(i => i!.Value)
.Sum(i => i.Weight * Weight(i.Name));
public override string Part1()
{
@ -41,7 +42,7 @@ public sealed class Day07 : Day
{
node = start.Dequeue();
foreach (var (container, contained) in _rules)
if (contained.Any(i => i.HasValue && i.Value.Item2 == node) && p.Add(container))
if (contained.Any(i => i.HasValue && i.Value.Name == node) && p.Add(container))
start.Enqueue(container);
if (!start.Any()) break;

View File

@ -66,18 +66,12 @@ public sealed class Day11 : Day
private LifeGame()
{
Grid = Array.Empty<char[]>();
}
public int TotalSeated =>
Grid.Sum(l => l.Count(c => c == '#'));
private void PrintBoard()
{
Console.Clear();
foreach (var line in Grid)
Console.WriteLine(line);
}
public LifeGame StepPart1()
{
var next = new LifeGame { _h = _h, _w = _w, Grid = Grid.Select(s => s.ToArray()).ToArray() };
@ -94,20 +88,16 @@ public sealed class Day11 : Day
return next;
}
private char At(int y, int x)
{
return x < 0 || y < 0 || x >= _w || y >= _h ? '.' : Grid[y][x];
}
private char At(int y, int x) =>
x < 0 || y < 0 || x >= _w || y >= _h ? '.' : Grid[y][x];
private int CountAdjacent(int y, int x)
{
return new[]
private int CountAdjacent(int y, int x) =>
new[]
{
At(y - 1, x - 1), At(y - 1, x + 0), At(y - 1, x + 1),
At(y + 0, x - 1), At(y + 0, x + 1),
At(y + 1, x - 1), At(y + 1, x + 0), At(y + 1, x + 1)
}.Count(c => c == '#');
}
At(y - 1, x - 1), At(y - 1, x + 0), At(y - 1, x + 1),
At(y + 0, x - 1), At(y + 0, x + 1),
At(y + 1, x - 1), At(y + 1, x + 0), At(y + 1, x + 1)
}.Count(c => c == '#');
public LifeGame StepPart2()
{
@ -125,15 +115,13 @@ public sealed class Day11 : Day
return next;
}
private int CanSee(int y, int x)
{
return new[]
private int CanSee(int y, int x) =>
new[]
{
TraceRay(y, x, -1, -1), TraceRay(y, x, -1, +0), TraceRay(y, x, -1, +1),
TraceRay(y, x, +0, -1), TraceRay(y, x, +0, +1),
TraceRay(y, x, +1, -1), TraceRay(y, x, +1, +0), TraceRay(y, x, +1, +1)
}.Count(c => c == '#');
}
TraceRay(y, x, -1, -1), TraceRay(y, x, -1, +0), TraceRay(y, x, -1, +1),
TraceRay(y, x, +0, -1), TraceRay(y, x, +0, +1),
TraceRay(y, x, +1, -1), TraceRay(y, x, +1, +0), TraceRay(y, x, +1, +1)
}.Count(c => c == '#');
private char TraceRay(int y, int x, int dy, int dx)
{