From 9edb557cd9a1afbe47955b725f5161026fb0fb34 Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Thu, 23 Dec 2021 13:46:26 -0500 Subject: [PATCH] tidy up some warnings --- aoc2021/Day14.cs | 10 +- aoc2021/Day18.cs | 9 +- aoc2021/Day19.cs | 8 +- aoc2021/Day20.cs | 5 +- aoc2021/Day21.cs | 4 +- aoc2021/Utils.cs | 264 ----------------------------------------------- 6 files changed, 18 insertions(+), 282 deletions(-) diff --git a/aoc2021/Day14.cs b/aoc2021/Day14.cs index db931c2..e8ee2a4 100644 --- a/aoc2021/Day14.cs +++ b/aoc2021/Day14.cs @@ -7,7 +7,7 @@ public sealed class Day14 : Day { private readonly string _template; private readonly Dictionary _substitutionPairs; - + public Day14() : base(14, "Extended Polymerization") { _template = Input.First(); @@ -17,7 +17,7 @@ public sealed class Day14 : Day private string DoStep(string input) { var result = new StringBuilder(); - + for (var i = 0; i < input.Length - 1; i++) { var k = input.Substring(i, 2); @@ -46,7 +46,7 @@ public sealed class Day14 : Day moleculeCounts[k] = moleculeCounts.GetValueOrDefault(k) + 1; } - foreach (var i in Enumerable.Range(0, steps)) + foreach (var _ in Enumerable.Range(0, steps)) { var updated = new Dictionary(); foreach (var (molecule, count) in moleculeCounts) @@ -58,7 +58,7 @@ public sealed class Day14 : Day moleculeCounts = updated; } - + var elementCounts = new Dictionary(); foreach (var (molecule, count) in moleculeCounts) { @@ -81,7 +81,7 @@ public sealed class Day14 : Day .OrderByDescending(g => g.Count()) .Select(g => g.Count()) .ToList(); - + return most.First() - most.Last(); } diff --git a/aoc2021/Day18.cs b/aoc2021/Day18.cs index 35fe8ff..177d2ae 100644 --- a/aoc2021/Day18.cs +++ b/aoc2021/Day18.cs @@ -6,7 +6,7 @@ public sealed class Day18 : Day { private readonly List _fishes; - + public Day18() : base(18, "Snailfish") { _fishes = Input.ToList(); @@ -17,7 +17,7 @@ public sealed class Day18 : Day static Tree.Node ParseFish(Tree.Node? parent, string input, ref int cursor) { if (input[cursor] != '[') return new(parent, input[cursor++] - '0'); - + var node = new Tree.Node(parent, -1); cursor++; node.Left = ParseFish(node, input, ref cursor); @@ -64,8 +64,7 @@ public sealed class Day18 : Day { if (reducer(tree, node)) return true; if (node.Left != null && ReduceRecurse(node.Left, reducer)) return true; - if (node.Right != null && ReduceRecurse(node.Right, reducer)) return true; - return false; + return node.Right != null && ReduceRecurse(node.Right, reducer); } bool Explode(Tree t, Tree.Node node) @@ -112,7 +111,7 @@ public sealed class Day18 : Day public override object Part2() { var best = 0L; - for (var i = 0; i < _fishes.Count; i++) + for (var i = 0; i < _fishes.Count; i++) best = _fishes .Where((_, j) => i != j) .Select(t => Add(Parse(_fishes[i]), Parse(t))) diff --git a/aoc2021/Day19.cs b/aoc2021/Day19.cs index 9eb1ff7..0eade87 100644 --- a/aoc2021/Day19.cs +++ b/aoc2021/Day19.cs @@ -55,13 +55,13 @@ public sealed class Day19 : Day private static Vector3 Translate(Vector3 p, Vector3 v) => (p.X + v.X, p.Y + v.Y, p.Z + v.Z); private static Vector3 Difference(Vector3 p1, Vector3 p2) => (p1.X - p2.X, p1.Y - p2.Y, p1.Z - p2.Z); - + private static int Manhattan(Vector3 a, Vector3 b) { var (dx, dy, dz) = Difference(a, b); return Math.Abs(dx) + Math.Abs(dy) + Math.Abs(dz); } - + private static (HashSet alignedBeacons, Vector3 translation, Vector3 up, int rotation)? Align( HashSet beacons1, IReadOnlyCollection beacons2) { @@ -120,7 +120,7 @@ public sealed class Day19 : Day // Skip all scans and scanners that were merged return (scans.Where((_, i) => !toRemove.Contains(i)).ToList(), - scanners.Where((el, i) => !toRemove.Contains(i)).ToList()); + scanners.Where((_, i) => !toRemove.Contains(i)).ToList()); } @@ -130,7 +130,7 @@ public sealed class Day19 : Day _scanners = scans.Select(_ => new HashSet { (0, 0, 0) }).ToList(); while (scans.Count > 1) (scans, _scanners) = Reduce(scans, _scanners); - + return scans[0].Count; } diff --git a/aoc2021/Day20.cs b/aoc2021/Day20.cs index 0c54458..d33de4a 100644 --- a/aoc2021/Day20.cs +++ b/aoc2021/Day20.cs @@ -70,9 +70,10 @@ public sealed class Day20 : Day public int GetEnhanceInput(Point pt) { + var (x, y) = pt; var values = - Enumerable.Range(pt.Y - 1, 3) - .SelectMany(_ => Enumerable.Range(pt.X - 1, 3), (yi, xi) => this[new(xi, yi)] ? 1 : 0); + Enumerable.Range(y - 1, 3) + .SelectMany(_ => Enumerable.Range(x - 1, 3), (yi, xi) => this[new(xi, yi)] ? 1 : 0); return values.Aggregate(0, (p, n) => (p << 1) | n); } diff --git a/aoc2021/Day21.cs b/aoc2021/Day21.cs index 4f74632..cae9f07 100644 --- a/aoc2021/Day21.cs +++ b/aoc2021/Day21.cs @@ -50,7 +50,7 @@ public sealed class Day21 : Day { var pts = MoveSpaces(key, player1Pos); if (player1Points + pts < 21) - RollDiracDie(player1Points + pts, player2Points, pts, player2Pos, 2, (value * universes)); + RollDiracDie(player1Points + pts, player2Points, pts, player2Pos, 2, value * universes); else _player1Victories += universes * value; } @@ -59,7 +59,7 @@ public sealed class Day21 : Day { var pts = MoveSpaces(key, player2Pos); if (player2Points + pts < 21) - RollDiracDie(player1Points, player2Points + pts, player1Pos, pts, 1, (value * universes)); + RollDiracDie(player1Points, player2Points + pts, player1Pos, pts, 1, value * universes); else _player2Victories += universes * value; } diff --git a/aoc2021/Utils.cs b/aoc2021/Utils.cs index 78cce81..5871721 100644 --- a/aoc2021/Utils.cs +++ b/aoc2021/Utils.cs @@ -2,20 +2,8 @@ namespace aoc2021; public class DefaultDict : Dictionary where TKey : notnull { - public DefaultDict() - { - } - - public DefaultDict(IDictionary dict) : base(dict) - { - } - public TValue? DefaultValue; - public DefaultDict(IEnumerable> pairs) : base(pairs) - { - } - public new TValue? this[TKey key] { get => TryGetValue(key, out var t) ? t : DefaultValue; @@ -90,178 +78,6 @@ public class Dijkstra where TCell : notnull public Func? Distance; public Func? Cell; - public Dictionary ComputeAll(TCell start, IEnumerable all) - { - var dist = new Dictionary(); - foreach (var cell in all) - { - dist[cell] = int.MaxValue; - } - - dist[start] = 0; - var queue = new PriorityQueue(dist.Select(pair => (pair.Key, pair.Value))); - while (queue.Count > 0) - { - var cell = queue.Dequeue(); - var current = dist[cell]; - foreach (var neighbor in Neighbors!(cell)) - { - var other = Cell!(cell, neighbor); - var weight = Distance!(neighbor); - if (current + weight < dist[other]) - { - dist[other] = current + weight; - } - } - } - - return dist; - } - - public Dictionary Compute(TCell start) - { - var dist = new DefaultDict { DefaultValue = int.MaxValue, [start] = 0 }; - var seen = new HashSet(); - var queue = new PriorityQueue(); - queue.Enqueue(start, 0); - while (queue.Count > 0) - { - var cell = queue.Dequeue(); - if (seen.Contains(cell)) continue; - seen.Add(cell); - var current = dist[cell]; - foreach (var neighbor in Neighbors!(cell)) - { - var other = Cell!(cell, neighbor); - var weight = Distance!(neighbor); - if (!seen.Contains(other)) queue.Enqueue(other, current + weight); - if (current + weight < dist[other]) - { - dist[other] = current + weight; - } - } - } - - return dist; - } - - public int Count(TCell start, Func count) - { - var total = 0; - var seen = new HashSet(); - var queue = new Queue(); - queue.Enqueue(start); - while (queue.Count > 0) - { - var cell = queue.Dequeue(); - if (seen.Contains(cell)) continue; - seen.Add(cell); - foreach (var neighbor in Neighbors!(cell)) - { - var other = Cell!(cell, neighbor); - if (count(other)) - { - total++; - continue; - } - - if (!seen.Contains(other)) queue.Enqueue(other); - } - } - - return total; - } - - public Dictionary ComputeWhere(TCell start, Func valid) - { - var dist = new DefaultDict { DefaultValue = int.MaxValue, [start] = 0 }; - var seen = new HashSet(); - var queue = new PriorityQueue(); - queue.Enqueue(start, 0); - while (queue.Count > 0) - { - var cell = queue.Dequeue(); - if (seen.Contains(cell)) continue; - seen.Add(cell); - var current = dist[cell]; - foreach (var neighbor in Neighbors!(cell)) - { - var other = Cell!(cell, neighbor); - if (!valid(other)) continue; - var weight = Distance!(neighbor); - if (!seen.Contains(other)) queue.Enqueue(other, current + weight); - if (current + weight < dist[other]) - { - dist[other] = current + weight; - } - } - } - - return dist; - } - - public Dictionary ComputeFrom(TCell start, Func? valid = null) - { - valid ??= _ => true; - var dist = new DefaultDict - { DefaultValue = (int.MaxValue, default)!, [start] = (0, default)! }; - var seen = new HashSet(); - var queue = new PriorityQueue(); - queue.Enqueue(start, 0); - while (queue.Count > 0) - { - var cell = queue.Dequeue(); - if (seen.Contains(cell)) continue; - seen.Add(cell); - var current = dist[cell].Dist; - foreach (var neighbor in Neighbors!(cell)) - { - var other = Cell!(cell, neighbor); - if (!valid(other)) continue; - var weight = Distance!(neighbor); - if (!seen.Contains(other)) queue.Enqueue(other, current + weight); - if (current + weight < dist[other].Dist) - { - dist[other] = (current + weight, cell); - } - } - } - - return dist; - } - - public Dictionary ComputePath(TCell start, TCell target, - Func? valid = null) - { - valid ??= _ => true; - var dist = new DefaultDict - { DefaultValue = (int.MaxValue, default)!, [start] = (0, default)! }; - var seen = new HashSet(); - var queue = new PriorityQueue(); - queue.Enqueue(start, 0); - while (queue.Count > 0) - { - var cell = queue.Dequeue(); - if (seen.Contains(cell)) continue; - seen.Add(cell); - if (Equals(cell, target)) return dist; - var current = dist[cell].Dist; - foreach (var neighbor in Neighbors!(cell)) - { - var other = Cell!(cell, neighbor); - if (!valid(other)) continue; - var weight = Distance!(neighbor); - if (!seen.Contains(other)) queue.Enqueue(other, current + weight); - if (current + weight < dist[other].Dist) - { - dist[other] = (current + weight, cell); - } - } - } - - return dist; - } - public int ComputeFind(TCell start, TCell target, Func? valid = null) { valid ??= _ => true; @@ -291,84 +107,4 @@ public class Dijkstra where TCell : notnull return -1; } -} - -public interface IDijkstra -{ - IEnumerable GetNeighbors(TCell cell); - - int GetWeight(TMid mid); - - TCell GetNeighbor(TCell from, TMid mid); -} - -public static class DijkstraExtensions -{ - private static Dijkstra Build(this IDijkstra dijkstra) where TCell : notnull - { - return new() - { - Neighbors = dijkstra.GetNeighbors, - Distance = dijkstra.GetWeight, - Cell = dijkstra.GetNeighbor - }; - } - - public static Dijkstra ToDijkstra(this IDijkstra dijkstra) - where TCell : notnull - { - return dijkstra.Build(); - } - - public static Dictionary DijkstraAll(this IDijkstra dijkstra, TCell start, - IEnumerable all) where TCell : notnull - { - return dijkstra.Build().ComputeAll(start, all); - } - - public static Dictionary Dijkstra(this IDijkstra dijkstra, TCell start) - where TCell : notnull - { - return dijkstra.Build().Compute(start); - } - - public static Dictionary DijkstraWhere(this IDijkstra dijkstra, TCell start, - Func valid) where TCell : notnull - { - return dijkstra.Build().ComputeWhere(start, valid); - } - - public static Dictionary DijkstraFrom( - this IDijkstra dijkstra, TCell start, Func? valid = null) where TCell : notnull - { - return dijkstra.Build().ComputeFrom(start, valid); - } - - public static int DijkstraFind(this IDijkstra dijkstra, TCell start, TCell target, - Func? valid = null) where TCell : notnull - { - return dijkstra.Build().ComputeFind(start, target, valid); - } - - public static Dictionary DijkstraPath( - this IDijkstra dijkstra, TCell start, TCell target, Func? valid = null) - where TCell : notnull - { - return dijkstra.Build().ComputePath(start, target, valid); - } - - public static IReadOnlyCollection GetPathTo(this Dictionary dist, - TCell target) where TCell : notnull - { - var list = new LinkedList(); - list.AddFirst(target); - while (true) - { - if (!dist.TryGetValue(target, out var pair)) return Array.Empty(); - if (pair.Dist == 0) break; - list.AddFirst(target = pair.From); - } - - return list; - } } \ No newline at end of file