generated from MartinZikmund/template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from MartinZikmund/feature/aoc2024-day19
AoC 2024 Day 19 Solutions
- Loading branch information
Showing
3 changed files
with
95 additions
and
0 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 |
---|---|---|
@@ -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; | ||
} | ||
} |
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 |
---|---|---|
@@ -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]; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
r, wr, b, g, bwu, rb, gb, br | ||
|
||
brwrr | ||
bggr | ||
gbbr | ||
rrbgbr | ||
ubwu | ||
bwurrg | ||
brgr | ||
bbrgwb |