generated from mazharenko/aoc-agent-template-multipleyears
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
24b72dd
commit a2d2424
Showing
3 changed files
with
450 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,52 @@ | ||
using mazharenko.AoCAgent.Generator; | ||
|
||
namespace aoc.Year2024; | ||
|
||
[BypassNoExamples] | ||
internal partial class Day19 | ||
{ | ||
internal partial class Part1 | ||
{ | ||
public string Solve(string input) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
public (string[] patterns, string[] designs) Parse(string input) | ||
{ | ||
return Character.Letter.Many().Select(chars => new string(chars)) | ||
.ManyDelimitedBy(Span.EqualTo(", ")) | ||
.Block() | ||
.ThenBlock( | ||
Character.Letter.Many().Select(chars => new string(chars)) | ||
.Lines() | ||
).Parse(input); | ||
} | ||
|
||
private static long GetWaysCounts(string[] patterns, string design) | ||
{ | ||
// How many ways to build the 0..i substring of <paramref name="design"/> from <paramref name="patterns"/> | ||
var dp = new long[design.Length + 1]; | ||
dp[0] = 1; | ||
for (var i = 1; i <= design.Length; i++) | ||
{ | ||
dp[i] = | ||
patterns.Where(pattern => | ||
design.AsSpan(0, i).EndsWith(pattern) | ||
).Sum( | ||
pattern => dp[i - pattern.Length] | ||
); | ||
} | ||
|
||
return dp[^1]; | ||
} | ||
|
||
internal partial class Part1 | ||
{ | ||
public int Solve((string[] patterns, string[] designs) input) | ||
{ | ||
return input.designs.Count(design => GetWaysCounts(input.patterns, design) > 0); | ||
} | ||
} | ||
|
||
internal partial class Part2 | ||
{ | ||
public string Solve(string input) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
internal partial class Part2 | ||
{ | ||
public long Solve((string[] patterns, string[] designs) input) | ||
{ | ||
return input.designs.Sum(design => GetWaysCounts(input.patterns, design)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.