From 8f642349256848c54db4ad4ee2875a2964ee40d2 Mon Sep 17 00:00:00 2001 From: Sebastian Lindemeier Date: Thu, 11 Dec 2025 08:39:25 +0100 Subject: [PATCH] Add Day11 implementation with puzzle-solving logic and tests --- AoC_2025.Tests/Day11Test.cs | 48 ++++++++++++++++++++++++ AoC_2025/Day11.cs | 74 +++++++++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 AoC_2025.Tests/Day11Test.cs create mode 100644 AoC_2025/Day11.cs diff --git a/AoC_2025.Tests/Day11Test.cs b/AoC_2025.Tests/Day11Test.cs new file mode 100644 index 0000000..93977cb --- /dev/null +++ b/AoC_2025.Tests/Day11Test.cs @@ -0,0 +1,48 @@ +using AdvenOfCode.Contracts; + +namespace AoC_2025.Tests; + +public class Day11Test +{ + private IPuzzleSolver _sut; + private static readonly string rootPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); + private readonly string TestInputPath = @$"{rootPath}\AoC-PuzzleInputs\2025\Test\day11.txt"; + private readonly string Test2InputPath = @$"{rootPath}\AoC-PuzzleInputs\2025\Test\day11_2.txt"; + private readonly string ProdInputPath = @$"{rootPath}\AoC-PuzzleInputs\2025\Prod\day11.txt"; + + public Day11Test() + { + _sut = new Day11(); + } + [Fact] + public void Part01_Test_equals_5() + { + var actual = _sut.SolvePart1(TestInputPath); + + Assert.Equal(5, actual); + } + + [Fact] + public void Part01_Prod_equals_494() + { + var actual = _sut.SolvePart1(ProdInputPath); + + Assert.Equal(494, actual); + } + + [Fact] + public void Part02_Test2_equals_2() + { + var actual = _sut.SolvePart2(Test2InputPath); + + Assert.Equal(2, actual); + } + + [Fact] + public void Part02_Prod_equals_296006754704850() + { + var actual = _sut.SolvePart2(ProdInputPath); + + Assert.Equal(296006754704850, actual); + } +} \ No newline at end of file diff --git a/AoC_2025/Day11.cs b/AoC_2025/Day11.cs new file mode 100644 index 0000000..2df206e --- /dev/null +++ b/AoC_2025/Day11.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.Common; +using System.IO; +using System.Linq; +using AdvenOfCode.Contracts; + +namespace AoC_2025; + +public class Day11 : IPuzzleSolver +{ + private Dictionary ParsePuzzleInput(string path) + { + var puzzleInput = File.ReadAllLines(path) + .Where(line => !string.IsNullOrWhiteSpace(line)) + .Select(line => line.Split(':', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)) + .Select(line => (line[0], line[1].Split(' ', StringSplitOptions.RemoveEmptyEntries))) + .ToDictionary(); + + return puzzleInput; + } + + public long SolvePart1(string pathToPuzzleInput) + { + var connections = ParsePuzzleInput(pathToPuzzleInput); + var pathsToOut = GetAmountPaths("you", "out", connections, []); + return pathsToOut; + } + + public long SolvePart2(string pathToPuzzleInput) + { + var connections = ParsePuzzleInput(pathToPuzzleInput); + var pathsToOut = GetAmountPathsThrough("svr", "out", ["dac", "fft"], connections, []); + return pathsToOut; + } + + private long GetAmountPaths(string from, string to, Dictionary paths, Dictionary<(string, string), long> memory) + { + if (from == to) return 1; + if (memory.TryGetValue((from, to), out var res)) return res; + + var totalAmount = 0L; + foreach (var path in paths[from]) + { + var amount = GetAmountPaths(path, to, paths, memory); + memory[(path, to)] = amount; + totalAmount += amount; + } + + memory[(from, to)] = totalAmount; + return totalAmount; + } + + private long GetAmountPathsThrough(string from, string to, HashSet through, + Dictionary paths, + Dictionary<(string, string), (long, HashSet)> memory) + { + through.Remove(from); + if (from == to) return through.Count == 0 ? 1 : 0; + if (memory.TryGetValue((from, to), out var res) && res.Item2.SetEquals(through)) return res.Item1; + + var totalAmount = 0L; + foreach (var path in paths[from]) + { + var amount = GetAmountPathsThrough(path, to, [..through], paths, memory); + memory[(path, to)] = (amount, through); + totalAmount += amount; + } + + memory[(from, to)] = (totalAmount, through); + return totalAmount; + } +} \ No newline at end of file