Skip to content

Commit

Permalink
Merge pull request #20 from MartinZikmund/feature/aoc2024-day19
Browse files Browse the repository at this point in the history
AoC 2024 Day 19 Solutions
  • Loading branch information
MartinZikmund authored Dec 20, 2024
2 parents ef6ce44 + b7e700a commit 25ef01b
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/AdventOfCode.Puzzles/2024/19/Part1/Part1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace AdventOfCode.Puzzles._2024._19.Part1;

public partial class Part1 : IPuzzleSolution
{
private string[] _patterns;

public async Task<string> 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;
}
}
44 changes: 44 additions & 0 deletions src/AdventOfCode.Puzzles/2024/19/Part2/Part2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
namespace AdventOfCode.Puzzles._2024._19.Part2;

public partial class Part2 : IPuzzleSolution
{
private HashSet<string> _patterns;

public async Task<string> SolveAsync(StreamReader inputReader)
{
_patterns = new HashSet<string>((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];
}
}
10 changes: 10 additions & 0 deletions src/AdventOfCode.Puzzles/2024/19/TestData.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
r, wr, b, g, bwu, rb, gb, br

brwrr
bggr
gbbr
rrbgbr
ubwu
bwurrg
brgr
bbrgwb

0 comments on commit 25ef01b

Please sign in to comment.