Skip to content

Commit

Permalink
day 19 WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
KaterynaKateryna committed Dec 19, 2024
1 parent 85deacc commit 6e7fd2e
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion AdventOfCode/Day19.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ public class Day19 : BaseDay
string[] _towels;
string[] _patterns;

Dictionary<char, List<string>> _towelsByFirstLetter = new Dictionary<char, List<string>>();

public Day19()
{
string[] lines = File.ReadAllLines(InputFilePath);
Expand All @@ -15,6 +17,14 @@ public Day19()
{
_patterns[i-2] = lines[i].Trim();
}

foreach (string towel in _towels)
{
if (!_towelsByFirstLetter.TryAdd(towel[0], new List<string> { towel }))
{
_towelsByFirstLetter[towel[0]].Add(towel);
}
}
}

public override ValueTask<string> Solve_1()
Expand All @@ -26,7 +36,11 @@ public override ValueTask<string> Solve_1()

public override ValueTask<string> Solve_2()
{
return new("test");
int result = _patterns.Sum(p => CountPatterns(p, 0));

//int result = CountPatterns(_patterns[0], 0);

return new(result.ToString());
}

private bool IsPatternPossible(string pattern, int position)
Expand Down Expand Up @@ -58,4 +72,38 @@ private bool IsPatternPossible(string pattern, int position)

return false;
}

private int CountPatterns(string pattern, int position)
{
if (position == pattern.Length)
{
return 1;
}

int res = 0;
if (_towelsByFirstLetter.TryGetValue(pattern[position], out var towelsToCheck))
{
foreach (string towel in towelsToCheck)
{
if (towel.Length <= pattern.Length - position)
{
bool matches = true;
for (int i = 0; i < towel.Length; ++i)
{
if (pattern[position + i] != towel[i])
{
matches = false;
break;
}
}
if (matches)
{
res += CountPatterns(pattern, position + towel.Length);
}
}
}
}

return res;
}
}

0 comments on commit 6e7fd2e

Please sign in to comment.