Refactor Day11 to handle missing paths in `paths` dictionary and improve variable usage readability

This commit is contained in:
Sebastian Lindemeier 2025-12-11 08:55:02 +01:00
parent 8f64234925
commit 3d305d80bf
1 changed files with 4 additions and 2 deletions

View File

@ -39,9 +39,10 @@ public class Day11 : IPuzzleSolver<long>
{ {
if (from == to) return 1; if (from == to) return 1;
if (memory.TryGetValue((from, to), out var res)) return res; if (memory.TryGetValue((from, to), out var res)) return res;
if (!paths.TryGetValue(from, out var pathsToCheck)) return 0;
var totalAmount = 0L; var totalAmount = 0L;
foreach (var path in paths[from]) foreach (var path in pathsToCheck)
{ {
var amount = GetAmountPaths(path, to, paths, memory); var amount = GetAmountPaths(path, to, paths, memory);
memory[(path, to)] = amount; memory[(path, to)] = amount;
@ -59,9 +60,10 @@ public class Day11 : IPuzzleSolver<long>
through.Remove(from); through.Remove(from);
if (from == to) return through.Count == 0 ? 1 : 0; if (from == to) return through.Count == 0 ? 1 : 0;
if (memory.TryGetValue((from, to), out var res) && res.Item2.SetEquals(through)) return res.Item1; if (memory.TryGetValue((from, to), out var res) && res.Item2.SetEquals(through)) return res.Item1;
if (!paths.TryGetValue(from, out var pathsToCheck)) return 0;
var totalAmount = 0L; var totalAmount = 0L;
foreach (var path in paths[from]) foreach (var path in pathsToCheck)
{ {
var amount = GetAmountPathsThrough(path, to, [..through], paths, memory); var amount = GetAmountPathsThrough(path, to, [..through], paths, memory);
memory[(path, to)] = (amount, through); memory[(path, to)] = (amount, through);