From b7e700ad34eae8e300bd41870c831688e541c850 Mon Sep 17 00:00:00 2001 From: Martin Zikmund Date: Fri, 20 Dec 2024 09:32:28 +0100 Subject: [PATCH] AoC 2024 Day 19 Solutions --- .../2024/19/Part1/Part1.cs | 41 +++++++++++++++++ .../2024/19/Part2/Part2.cs | 44 +++++++++++++++++++ src/AdventOfCode.Puzzles/2024/19/TestData.txt | 10 +++++ 3 files changed, 95 insertions(+) create mode 100644 src/AdventOfCode.Puzzles/2024/19/Part1/Part1.cs create mode 100644 src/AdventOfCode.Puzzles/2024/19/Part2/Part2.cs create mode 100644 src/AdventOfCode.Puzzles/2024/19/TestData.txt diff --git a/src/AdventOfCode.Puzzles/2024/19/Part1/Part1.cs b/src/AdventOfCode.Puzzles/2024/19/Part1/Part1.cs new file mode 100644 index 0000000..7c5dcb5 --- /dev/null +++ b/src/AdventOfCode.Puzzles/2024/19/Part1/Part1.cs @@ -0,0 +1,41 @@ +namespace AdventOfCode.Puzzles._2024._19.Part1; + +public partial class Part1 : IPuzzleSolution +{ + private string[] _patterns; + + public async Task SolveAsync(StreamReader inputReader) + { + _patterns = (await inputReader.ReadLineAsync()).Split(", "); + await inputReader.ReadLineAsync(); + int possibleCount = 0; + while (await inputReader.ReadLineAsync() is { } towel) + { + if (IsPossible("", towel)) + { + possibleCount++; + } + } + + return possibleCount.ToString(); + } + + private bool IsPossible(string current, string towel) + { + if (current == towel) + { + return true; + } + + for (int i = 0; i < _patterns.Length; i++) + { + var newStart = current + _patterns[i]; + if (towel.StartsWith(newStart) && IsPossible(newStart, towel)) + { + return true; + } + } + + return false; + } +} diff --git a/src/AdventOfCode.Puzzles/2024/19/Part2/Part2.cs b/src/AdventOfCode.Puzzles/2024/19/Part2/Part2.cs new file mode 100644 index 0000000..6753e5b --- /dev/null +++ b/src/AdventOfCode.Puzzles/2024/19/Part2/Part2.cs @@ -0,0 +1,44 @@ +namespace AdventOfCode.Puzzles._2024._19.Part2; + +public partial class Part2 : IPuzzleSolution +{ + private HashSet _patterns; + + public async Task SolveAsync(StreamReader inputReader) + { + _patterns = new HashSet((await inputReader.ReadLineAsync()).Split(", ")); + await inputReader.ReadLineAsync(); + ulong possibleCount = 0; + while (await inputReader.ReadLineAsync() is { } towel) + { + possibleCount += CountPossible(towel); + } + + return possibleCount.ToString(); + } + + private ulong CountPossible(string towel) + { + ulong[] ways = new ulong[towel.Length + 1]; + ways[0] = 1; + + for (var length = 1; length <= towel.Length; length++) + { + for (int previousLength = 0; previousLength < length; previousLength++) + { + if (ways[previousLength] == 0) + { + continue; + } + + var current = towel.Substring(previousLength, length - previousLength); + if (_patterns.Contains(current)) + { + ways[length] += ways[previousLength]; + } + } + } + + return ways[towel.Length]; + } +} diff --git a/src/AdventOfCode.Puzzles/2024/19/TestData.txt b/src/AdventOfCode.Puzzles/2024/19/TestData.txt new file mode 100644 index 0000000..29648be --- /dev/null +++ b/src/AdventOfCode.Puzzles/2024/19/TestData.txt @@ -0,0 +1,10 @@ +r, wr, b, g, bwu, rb, gb, br + +brwrr +bggr +gbbr +rrbgbr +ubwu +bwurrg +brgr +bbrgwb