|
| 1 | +namespace AdventOfCode.Year2021.Solvers.Day15; |
| 2 | + |
| 3 | +public abstract class Day15SolverBase : SolverWithArrayInput<byte[], int> |
| 4 | +{ |
| 5 | + protected override int Solve(byte[][] costs) |
| 6 | + { |
| 7 | + return new InnerSolver(costs, Cost, DimensionsExtendMultiplier).Solve(); |
| 8 | + } |
| 9 | + |
| 10 | + protected abstract int DimensionsExtendMultiplier { get; } |
| 11 | + protected abstract int Cost((int row, int col) location, byte[][] costs); |
| 12 | + |
| 13 | + |
| 14 | + private class InnerSolver |
| 15 | + { |
| 16 | + private readonly int _dimensionsExtendMultiplier; |
| 17 | + private readonly Func<(int row, int col), int> _getCost; |
| 18 | + private readonly int _height; |
| 19 | + private readonly int _width; |
| 20 | + |
| 21 | + public InnerSolver( |
| 22 | + byte[][] costs, |
| 23 | + Func<(int row, int col), byte[][], int> getCost, |
| 24 | + int dimensionsExtendMultiplier |
| 25 | + ) |
| 26 | + { |
| 27 | + _height = costs.Length; |
| 28 | + _width = costs[0].Length; |
| 29 | + _getCost = l => getCost(l, costs); |
| 30 | + _dimensionsExtendMultiplier = dimensionsExtendMultiplier; |
| 31 | + } |
| 32 | + |
| 33 | + public int Solve() |
| 34 | + { |
| 35 | + var startLocation = (0, 0); |
| 36 | + var extension = new HashSet<(int row, int col)> { startLocation }; |
| 37 | + var reached = new Dictionary<(int, int), int> { { startLocation, 0 } }; |
| 38 | + |
| 39 | + while (extension.Any()) |
| 40 | + { |
| 41 | + extension = Extend(extension, reached); |
| 42 | + } |
| 43 | + |
| 44 | + var endLocation = (_height * _dimensionsExtendMultiplier - 1, _width * _dimensionsExtendMultiplier - 1); |
| 45 | + return reached[endLocation]; |
| 46 | + } |
| 47 | + |
| 48 | + private HashSet<(int, int)> Extend( |
| 49 | + HashSet<(int, int)> prevExtension, |
| 50 | + Dictionary<(int, int), int> reached |
| 51 | + ) |
| 52 | + { |
| 53 | + var extension = new HashSet<(int, int)>(); |
| 54 | + foreach (var location in prevExtension) |
| 55 | + { |
| 56 | + int locationTotalCost = reached[location]; |
| 57 | + ForAdjacent(location, a => processAdjacent(a, locationTotalCost)); |
| 58 | + } |
| 59 | + return extension; |
| 60 | + |
| 61 | + void processAdjacent((int row, int col) adjacent, int locationTotalCost) |
| 62 | + { |
| 63 | + int newAdjacentTotalCost = _getCost(adjacent) + locationTotalCost; |
| 64 | + if (reached.TryGetValue(adjacent, out int adjacentTotalCost)) |
| 65 | + { |
| 66 | + if (newAdjacentTotalCost >= adjacentTotalCost) return; |
| 67 | + reached[adjacent] = newAdjacentTotalCost; |
| 68 | + } |
| 69 | + else |
| 70 | + { |
| 71 | + reached.Add(adjacent, newAdjacentTotalCost); |
| 72 | + } |
| 73 | + extension.Add(adjacent); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private void ForAdjacent((int, int) location, Action<(int, int)> action) |
| 78 | + { |
| 79 | + int height = _height * _dimensionsExtendMultiplier; |
| 80 | + int width = _width * _dimensionsExtendMultiplier; |
| 81 | + |
| 82 | + (int row, int col) = location; |
| 83 | + if (row > 0) action((row - 1, col)); |
| 84 | + if (row < height - 1) action((row + 1, col)); |
| 85 | + if (col > 0) action((row, col - 1)); |
| 86 | + if (col < width - 1) action((row, col + 1)); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments