Add Day11 implementation with puzzle-solving logic and tests
This commit is contained in:
parent
6621a36021
commit
8f64234925
|
|
@ -0,0 +1,48 @@
|
||||||
|
using AdvenOfCode.Contracts;
|
||||||
|
|
||||||
|
namespace AoC_2025.Tests;
|
||||||
|
|
||||||
|
public class Day11Test
|
||||||
|
{
|
||||||
|
private IPuzzleSolver<long> _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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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<long>
|
||||||
|
{
|
||||||
|
private Dictionary<string, string[]> 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<string, string[]> 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<string> through,
|
||||||
|
Dictionary<string, string[]> paths,
|
||||||
|
Dictionary<(string, string), (long, HashSet<string>)> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue