74 lines
2.5 KiB
C#
74 lines
2.5 KiB
C#
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;
|
|
}
|
|
} |