From 260e10ec9e418c12ff765953d24a5202457ccbf4 Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Fri, 1 Dec 2023 13:21:24 -0500 Subject: [PATCH] clean up some warnings --- AOC.Common/Extensions.cs | 3 ++- AOC.Common/Trees.cs | 15 ++++++++++---- AOC2015/Day12.cs | 4 ++-- AOC2015/Day16.cs | 7 +++++-- AOC2015/Day22.cs | 7 ++++--- AOC2016/Day07.cs | 22 ++++++++------------ AOC2019/Day05.cs | 6 ++++-- AOC2019/Day10.cs | 12 +++++------ AOC2019/Day14.cs | 6 +++--- AOC2020/Day07.cs | 2 +- AOC2020/Day14.cs | 7 +++++-- AOC2020/Day17.cs | 4 ++-- AOC2020/Day18.cs | 43 +++++++++++++++++++++++++--------------- AOC2020/Day20.cs | 27 ++++++++++++++----------- AOC2021/Day03.cs | 6 +++--- AOC2021/Day08.cs | 3 ++- AOC2021/Day09.cs | 12 +++++------ AOC2021/Day12.cs | 4 ++-- AOC2021/Day14.cs | 4 ++-- AOC2023/Day01.cs | 4 ++-- AdventOfCode.sln | 3 +++ 21 files changed, 115 insertions(+), 86 deletions(-) diff --git a/AOC.Common/Extensions.cs b/AOC.Common/Extensions.cs index d47762c..16e3e6e 100644 --- a/AOC.Common/Extensions.cs +++ b/AOC.Common/Extensions.cs @@ -8,6 +8,7 @@ public static class Extensions public static IEnumerable Repeat(this IEnumerable sequence, int? count = null) { while (count == null || count-- > 0) + // ReSharper disable once PossibleMultipleEnumeration foreach (var item in sequence) yield return item; } @@ -123,4 +124,4 @@ public static class Extensions if (s1.Length != other.Length) throw new("Strings must be equal length."); return s1.Zip(other).Count(s => s.First != s.Second); } -} \ No newline at end of file +} diff --git a/AOC.Common/Trees.cs b/AOC.Common/Trees.cs index 07351e8..0ffa0bd 100644 --- a/AOC.Common/Trees.cs +++ b/AOC.Common/Trees.cs @@ -41,17 +41,24 @@ public class Tree(Tree.Node root) set { if (value != null) value.Parent = this; - if (Children.Count >= 2) Children[1] = value; - else if (Children.Count == 0) Children.Add(null); + switch (Children.Count) + { + case >= 2: + Children[1] = value; + break; + case 0: + Children.Add(null); + break; + } Children.Add(value); } } - public int DistanceToParent(Node parent) + public int DistanceToParent(Node nodeParent) { var current = this; var dist = 0; - while (current != parent) + while (current != nodeParent) { dist++; current = current?.Parent; diff --git a/AOC2015/Day12.cs b/AOC2015/Day12.cs index fc2ac80..f852582 100644 --- a/AOC2015/Day12.cs +++ b/AOC2015/Day12.cs @@ -35,5 +35,5 @@ public sealed partial class Day12() : Day(2015, 12, "JSAbacusFramework.io") private long GetSum(JArray arr, string avoid) => arr.Sum((dynamic a) => (long)GetSum(a, avoid)); - private long GetSum(JValue val, string avoid) => val.Type == JTokenType.Integer ? (long)(val.Value ?? 0L) : 0; -} \ No newline at end of file + private static long GetSum(JValue val, string avoid) => val.Type == JTokenType.Integer ? (long)(val.Value ?? 0L) : 0; +} diff --git a/AOC2015/Day16.cs b/AOC2015/Day16.cs index f4862bd..6341177 100644 --- a/AOC2015/Day16.cs +++ b/AOC2015/Day16.cs @@ -3,15 +3,18 @@ namespace AOC2015; /// /// Day 16: /// -public sealed class Day16() : Day(2015, 16, "Aunt Sue") +public sealed partial class Day16() : Day(2015, 16, "Aunt Sue") { + [GeneratedRegex(@": \d\d")] + private static partial Regex TwoDigitsRegex(); + public override void ProcessInput() { } private IEnumerable Common() => Input - .Select(i => Regex.Replace(i, @": \d\d", ": 9")) + .Select(i => TwoDigitsRegex().Replace(i, ": 9")) .WhereMatch("children: 3") .WhereMatch("samoyeds: 2") .WhereMatch("akitas: 0") diff --git a/AOC2015/Day22.cs b/AOC2015/Day22.cs index df43a24..2ee855d 100644 --- a/AOC2015/Day22.cs +++ b/AOC2015/Day22.cs @@ -18,7 +18,8 @@ public sealed class Day22() : Day(2015, 22, "Wizard Simulator 20XX") private Dictionary _boss; - public record Spell( + private record Spell( + // ReSharper disable once NotAccessedPositionalProperty.Local string Name, int Mana, int Duration = 0, @@ -28,7 +29,7 @@ public sealed class Day22() : Day(2015, 22, "Wizard Simulator 20XX") int ManaCharge = 0 ); - public struct GameState(bool HardMode = false, int RoundNumber = 0, int TotalManaSpent = 0, int PlayerHealth = 50, + private struct GameState(bool HardMode = false, int RoundNumber = 0, int TotalManaSpent = 0, int PlayerHealth = 50, int PlayerMana = 500, int BossHealth = 0, int BossDamage = 0, Dictionary? ActiveSpells = null) { public GameResult DoTurn(Spell spell) @@ -77,7 +78,7 @@ public sealed class Day22() : Day(2015, 22, "Wizard Simulator 20XX") } } - public enum GameResult + private enum GameResult { Win, Loss, diff --git a/AOC2016/Day07.cs b/AOC2016/Day07.cs index de96181..53ce042 100644 --- a/AOC2016/Day07.cs +++ b/AOC2016/Day07.cs @@ -15,15 +15,9 @@ public sealed partial class Day07() : Day(2016, 7, "Internet Protocol Version 7" { } - private static bool SupportsTls(string input) - { - foreach (var m in InsideBracketsRegex().Matches(input).Cast()) - { - if (CheckAbba(m.ValueSpan)) return false; - } - - return BracketsRegex().Split(input).Any(v => CheckAbba(v)); - } + private static bool SupportsTls(string input) => + !InsideBracketsRegex().Matches(input).Any(m => CheckAbba(m.ValueSpan)) && + BracketsRegex().Split(input).Any(v => CheckAbba(v)); private static bool CheckAbba(ReadOnlySpan input) { @@ -41,10 +35,10 @@ public sealed partial class Day07() : Day(2016, 7, "Internet Protocol Version 7" private static bool SupportsSsl(string input) { foreach (var ip in BracketsRegex().Split(input)) - foreach (var aba in CheckAba(ip)) - foreach (var m in InsideBracketsRegex().Matches(input).Cast()) - if (m.Value.Contains($"{aba[1]}{aba[0]}{aba[1]}")) - return true; + foreach (var aba in CheckAba(ip)) + foreach (var m in InsideBracketsRegex().Matches(input).Cast()) + if (m.Value.Contains($"{aba[1]}{aba[0]}{aba[1]}")) + return true; return false; } @@ -58,4 +52,4 @@ public sealed partial class Day07() : Day(2016, 7, "Internet Protocol Version 7" public override object Part1() => Input.Count(SupportsTls); public override object Part2() => Input.Count(SupportsSsl); -} \ No newline at end of file +} diff --git a/AOC2019/Day05.cs b/AOC2019/Day05.cs index 887fd7c..2cfd44a 100644 --- a/AOC2019/Day05.cs +++ b/AOC2019/Day05.cs @@ -13,8 +13,6 @@ public sealed class Day05() : Day(2019, 5, "Sunny with a Chance of Asteroids") var i = 0; while (i < v.Count && v[i] != 99) { - int Val(int mode, int val) => mode != 0 ? val : v[val]; - var mode1 = v[i] / 100 % 10; var mode2 = v[i] / 1000; @@ -51,6 +49,10 @@ public sealed class Day05() : Day(2019, 5, "Sunny with a Chance of Asteroids") i += 4; break; } + + continue; + + int Val(int mode, int val) => mode != 0 ? val : v[val]; } } diff --git a/AOC2019/Day10.cs b/AOC2019/Day10.cs index 15715f7..db2f200 100644 --- a/AOC2019/Day10.cs +++ b/AOC2019/Day10.cs @@ -36,12 +36,6 @@ public sealed class Day10() : Day(2019, 10, "Monitoring Station") public override object Part2() { - static IEnumerable<(int x, int y, double angle, double dist)> GetValue( - Queue<(int x, int y, double angle, double dist)> q) - { - if (q.Count > 0) yield return q.Dequeue(); - } - return _asteroids! .Where(a => a != _best) .Select(a => @@ -60,5 +54,11 @@ public sealed class Day10() : Day(2019, 10, "Monitoring Station") .Take(1) .Select(a => a.x * 100 + a.y) .Single(); + + static IEnumerable<(int x, int y, double angle, double dist)> GetValue( + Queue<(int x, int y, double angle, double dist)> q) + { + if (q.Count > 0) yield return q.Dequeue(); + } } } diff --git a/AOC2019/Day14.cs b/AOC2019/Day14.cs index 0430599..e8cd097 100644 --- a/AOC2019/Day14.cs +++ b/AOC2019/Day14.cs @@ -12,8 +12,7 @@ public sealed class Day14() : Day(2019, 14, "Space Stoichiometry") private bool Consume(string chem, long quantity) { - if (quantity <= 0) - throw new ArgumentOutOfRangeException(nameof(quantity)); + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(quantity); _available.TryAdd(chem, 0); @@ -70,6 +69,7 @@ public sealed class Day14() : Day(2019, 14, "Space Stoichiometry") { public readonly Component Product; public readonly Component[] Reactants; + private static readonly string[] Separators = [", ", " => "]; private Reaction(Component[] reactants, Component product) { @@ -79,7 +79,7 @@ public sealed class Day14() : Day(2019, 14, "Space Stoichiometry") public static Reaction Parse(string s) { - var ss = s.Split(new[] { ", ", " => " }, StringSplitOptions.None); + var ss = s.Split(Separators, StringSplitOptions.None); return new( ss.Take(ss.Length - 1).Select(ParseComponent).ToArray(), diff --git a/AOC2020/Day07.cs b/AOC2020/Day07.cs index bb0270d..7c5e8bd 100644 --- a/AOC2020/Day07.cs +++ b/AOC2020/Day07.cs @@ -34,7 +34,7 @@ public sealed class Day07() : Day(2020, 7, "Handy Haversacks") public override object Part1() { // breadth-first search with Queue - var start = new Queue(new[] { "shiny gold" }); + Queue start = new(["shiny gold"]); var p = new HashSet(); string node; while (true) diff --git a/AOC2020/Day14.cs b/AOC2020/Day14.cs index 130882f..4aaa1f5 100644 --- a/AOC2020/Day14.cs +++ b/AOC2020/Day14.cs @@ -9,6 +9,9 @@ public sealed class Day14() : Day(2020, 14, "Docking Data") { } + private static readonly char[] SquareBrackets = ['[', ']']; + private static readonly char[] BracketsAndEquals = [..SquareBrackets, '=']; + public override object Part1() { var writes = new Dictionary(); @@ -32,7 +35,7 @@ public sealed class Day14() : Day(2020, 14, "Docking Data") } else { - var spl = line.Split(new[] { '[', ']', '=' }, + var spl = line.Split(BracketsAndEquals, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) .Skip(1) .Select(ulong.Parse) @@ -60,7 +63,7 @@ public sealed class Day14() : Day(2020, 14, "Docking Data") else { var value = ulong.Parse(spl[2]); - var addr = ulong.Parse(spl[0].Split(new[] { '[', ']' }, + var addr = ulong.Parse(spl[0].Split(SquareBrackets, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)[1]); var floats = new List(); diff --git a/AOC2020/Day17.cs b/AOC2020/Day17.cs index ba8c3ba..1eab088 100644 --- a/AOC2020/Day17.cs +++ b/AOC2020/Day17.cs @@ -58,7 +58,7 @@ public sealed class Day17() : Day(2020, 17, "Conway Cubes") { var active = Neighbors(prev, x, y, z); if (prev[(x, y, z)] == '#') - next[(x, y, z)] = active == 2 || active == 3 ? '#' : '.'; + next[(x, y, z)] = active is 2 or 3 ? '#' : '.'; else next[(x, y, z)] = active == 3 ? '#' : '.'; } @@ -95,7 +95,7 @@ public sealed class Day17() : Day(2020, 17, "Conway Cubes") { var active = Neighbors4(prev, x, y, z, w); if (prev[(x, y, z, w)] == '#') - next[(x, y, z, w)] = active == 2 || active == 3 ? '#' : '.'; + next[(x, y, z, w)] = active is 2 or 3 ? '#' : '.'; else next[(x, y, z, w)] = active == 3 ? '#' : '.'; } diff --git a/AOC2020/Day18.cs b/AOC2020/Day18.cs index 431e183..64a6409 100644 --- a/AOC2020/Day18.cs +++ b/AOC2020/Day18.cs @@ -20,23 +20,27 @@ public sealed class Day18() : Day(2020, 18, "Operation Order") { postfixNotation.Append(c); } - else if (c == '(') + else switch (c) { - postfixStack.Push(c); - } - else if (c == ')') - { - while (postfixStack.Count > 0 && postfixStack.Peek() != '(') - postfixNotation.Append(postfixStack.Pop()); + case '(': + postfixStack.Push(c); + break; + case ')': + { + while (postfixStack.Count > 0 && postfixStack.Peek() != '(') + postfixNotation.Append(postfixStack.Pop()); - postfixStack.TryPop(out _); - } - else - { - while (postfixStack.Count > 0 && precedence(c) <= precedence(postfixStack.Peek())) - postfixNotation.Append(postfixStack.Pop()); + postfixStack.TryPop(out _); + break; + } + default: + { + while (postfixStack.Count > 0 && precedence(c) <= precedence(postfixStack.Peek())) + postfixNotation.Append(postfixStack.Pop()); - postfixStack.Push(c); + postfixStack.Push(c); + break; + } } while (postfixStack.Count > 0) @@ -54,8 +58,15 @@ public sealed class Day18() : Day(2020, 18, "Operation Order") var a = expressionStack.Pop(); var b = expressionStack.Pop(); - if (c == '+') expressionStack.Push(a + b); - if (c == '*') expressionStack.Push(a * b); + switch (c) + { + case '+': + expressionStack.Push(a + b); + break; + case '*': + expressionStack.Push(a * b); + break; + } } return expressionStack.Pop(); diff --git a/AOC2020/Day20.cs b/AOC2020/Day20.cs index 6444592..ac8ab31 100644 --- a/AOC2020/Day20.cs +++ b/AOC2020/Day20.cs @@ -3,8 +3,11 @@ namespace AOC2020; /// /// Day 20: /// -public sealed class Day20() : Day(2020, 20, "Jurassic Jigsaw") +public sealed partial class Day20() : Day(2020, 20, "Jurassic Jigsaw") { + [GeneratedRegex("(?<=#.{77})#.{4}#{2}.{4}#{2}.{4}#{3}(?=.{77}#.{2}#.{2}#.{2}#.{2}#.{2}#)")] + private static partial Regex MonsterRegex(); + public override void ProcessInput() { } @@ -51,14 +54,6 @@ public sealed class Day20() : Day(2020, 20, "Jurassic Jigsaw") var sides = new Dictionary(); var connections = new Dictionary>(); - void AddConnection(PuzzlePiece p1, PuzzlePiece p2) - { - if (!connections.ContainsKey(p1)) connections.Add(p1, []); - if (!connections.ContainsKey(p2)) connections.Add(p2, []); - connections[p1].Add(p2); - connections[p2].Add(p1); - } - foreach (var piece in puzzlePieces) foreach (var (original, flipped) in piece.SidesWithFlippedPaired.Value) { @@ -78,6 +73,14 @@ public sealed class Day20() : Day(2020, 20, "Jurassic Jigsaw") } return connections; + + void AddConnection(PuzzlePiece p1, PuzzlePiece p2) + { + if (!connections.ContainsKey(p1)) connections.Add(p1, []); + if (!connections.ContainsKey(p2)) connections.Add(p2, []); + connections[p1].Add(p2); + connections[p2].Add(p1); + } } private static IEnumerable ComposePuzzle(Dictionary> connections) @@ -171,10 +174,8 @@ public sealed class Day20() : Day(2020, 20, "Jurassic Jigsaw") private static int CountSeaMonstersInImage(char[][] lines) { - const string pattern = @"(?<=#.{77})#.{4}#{2}.{4}#{2}.{4}#{3}(?=.{77}#.{2}#.{2}#.{2}#.{2}#.{2}#)"; var singleLine = lines.Aggregate("", (curr, next) => curr + new string(next)); - var matches = Regex.Matches(singleLine, pattern); - return matches.Count; + return MonsterRegex().Matches(singleLine).Count; } private class PuzzlePiece @@ -253,4 +254,6 @@ public sealed class Day20() : Day(2020, 20, "Jurassic Jigsaw") private static HashSet CalculateAllSidesWithFlipped(PuzzlePiece piece) => piece.Sides.Value.Concat(piece.Sides.Value.Select(s => new string(s.Reverse().ToArray()))).ToHashSet(); } + + } diff --git a/AOC2021/Day03.cs b/AOC2021/Day03.cs index a04c095..c58fec7 100644 --- a/AOC2021/Day03.cs +++ b/AOC2021/Day03.cs @@ -34,9 +34,6 @@ public sealed class Day03() : Day(2021, 3, "Binary Diagnostic") var o = _report!; var c = _report!; - char MostCommon(int i, IReadOnlyCollection report) => - report.Count(r => r[i] == '1') >= report.Count / 2.0 ? '1' : '0'; - var i = 0; while (o.Count > 1) { @@ -56,5 +53,8 @@ public sealed class Day03() : Day(2021, 3, "Binary Diagnostic") var co2 = c.Single().BigIntegerFromBinaryString(); return o2 * co2; + + char MostCommon(int index, IReadOnlyCollection report) => + report.Count(r => r[index] == '1') >= report.Count / 2.0 ? '1' : '0'; } } diff --git a/AOC2021/Day08.cs b/AOC2021/Day08.cs index cff8694..73f46b7 100644 --- a/AOC2021/Day08.cs +++ b/AOC2021/Day08.cs @@ -7,6 +7,7 @@ public sealed class Day08() : Day(2021, 8, "Seven Segment Search") { private static readonly List PossibleMappings = "abcdefg".ToCharArray().Permute().Select(m => m.ToArray()).ToList(); + private static readonly int[] SearchValues = { 2, 3, 4, 7 }; public override void ProcessInput() { @@ -78,7 +79,7 @@ public sealed class Day08() : Day(2021, 8, "Seven Segment Search") public override object Part1() => Input .Select(line => line.Split(" | ")[1].Split(' ')) - .Select(outputs => outputs.Count(o => new[] { 2, 3, 4, 7 }.Contains(o.Length))) + .Select(outputs => outputs.Count(o => SearchValues.Contains(o.Length))) .Sum(); public override object Part2() => Input.Select(Decode).Sum(); diff --git a/AOC2021/Day09.cs b/AOC2021/Day09.cs index a99d7db..c0f2503 100644 --- a/AOC2021/Day09.cs +++ b/AOC2021/Day09.cs @@ -41,12 +41,15 @@ public sealed class Day09() : Day(2021, 9, "Smoke Basin") var s = 0; var seen = new HashSet<(int x, int y)>(); + MoreEnumerable.TraverseBreadthFirst((x, y), Traverse).Consume(); + sizes.Add(s); + continue; + IEnumerable<(int x, int y)> Traverse((int x, int y) p) { var (i, j) = p; if (_map![j][i] == '9') yield break; - if (seen.Contains((i, j))) yield break; - seen.Add((i, j)); + if (!seen.Add((i, j))) yield break; s++; if (j > 0) @@ -58,9 +61,6 @@ public sealed class Day09() : Day(2021, 9, "Smoke Basin") if (i < _map[j].Length - 1) yield return (i + 1, j); } - - MoreEnumerable.TraverseBreadthFirst((x, y), Traverse).Consume(); - sizes.Add(s); } return sizes @@ -68,4 +68,4 @@ public sealed class Day09() : Day(2021, 9, "Smoke Basin") .Take(3) .Aggregate(1L, (a, b) => a * b); } -} \ No newline at end of file +} diff --git a/AOC2021/Day12.cs b/AOC2021/Day12.cs index 0478be0..32c4f62 100644 --- a/AOC2021/Day12.cs +++ b/AOC2021/Day12.cs @@ -13,10 +13,10 @@ public sealed class Day12() : Day(2021, 12, "Passage Pathing") { var s = line.Split('-', 2); - if (_edges.ContainsKey(s[0])) _edges[s[0]].Add(s[1]); + if (_edges.TryGetValue(s[0], out var edge)) edge.Add(s[1]); else _edges[s[0]] = [s[1]]; - if (_edges.ContainsKey(s[1])) _edges[s[1]].Add(s[0]); + if (_edges.TryGetValue(s[1], out var edge2)) edge2.Add(s[0]); else _edges[s[1]] = [s[0]]; } } diff --git a/AOC2021/Day14.cs b/AOC2021/Day14.cs index eb25659..adeab70 100644 --- a/AOC2021/Day14.cs +++ b/AOC2021/Day14.cs @@ -21,10 +21,10 @@ public sealed class Day14() : Day(2021, 14, "Extended Polymerization") for (var i = 0; i < input.Length - 1; i++) { var k = input.Substring(i, 2); - if (_substitutionPairs!.ContainsKey(k)) + if (_substitutionPairs!.TryGetValue(k, out var value)) { result.Append(k[0]); - result.Append(_substitutionPairs[k]); + result.Append(value); } else { diff --git a/AOC2023/Day01.cs b/AOC2023/Day01.cs index d93fecc..999f88b 100644 --- a/AOC2023/Day01.cs +++ b/AOC2023/Day01.cs @@ -2,7 +2,7 @@ namespace AOC2023; public class Day01() : Day(2023, 1, "Trebuchet?!") { - private static readonly List _singleDigits = + private static readonly List SingleDigits = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]; public override void ProcessInput() @@ -25,7 +25,7 @@ public class Day01() : Day(2023, 1, "Trebuchet?!") continue; } - foreach (var (digit, spelled) in _singleDigits.Indexed()) + foreach (var (digit, spelled) in SingleDigits.Indexed()) { if (i + spelled.Length - 1 < line.Length && line[i..(i + spelled.Length)] == spelled) { diff --git a/AdventOfCode.sln b/AdventOfCode.sln index 1938865..db862ee 100644 --- a/AdventOfCode.sln +++ b/AdventOfCode.sln @@ -27,9 +27,12 @@ EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{FD88DFA6-FE18-466D-B249-14FE2817D45A}" ProjectSection(SolutionItems) = preProject .drone.yml = .drone.yml + .editorconfig = .editorconfig + .gitignore = .gitignore DayXX.cs.txt = DayXX.cs.txt getday.sh = getday.sh global.json = global.json + LICENSE = LICENSE README.md = README.md EndProjectSection EndProject