-
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
383544f
commit 2780eb7
Showing
3 changed files
with
55 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,10 @@ | ||
r, wr, b, g, bwu, rb, gb, br | ||
|
||
brwrr | ||
bggr | ||
gbbr | ||
rrbgbr | ||
ubwu | ||
bwurrg | ||
brgr | ||
bbrgwb |
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
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 @@ | ||
import 'package:darq/darq.dart'; | ||
import 'package:h3x_devtools/solvers/advent_of_code/2024/aoc_2024_solver.dart'; | ||
|
||
class Day19Solver extends AdventOfCode2024Solver { | ||
|
||
@override | ||
final int dayNumber = 19; | ||
|
||
@override | ||
String getSolution(String input) { | ||
String splitNewline = input.contains('\r\n\r\n') ? '\r\n\r\n' : input.contains('\r\r') ? '\r\r' : '\n\n'; | ||
List<String> inputParts = input.split(splitNewline); | ||
|
||
List<String> towelPatterns = inputParts[0].split(', ').toList(); | ||
List<String> desiredDesigns = inputParts[1].toListOfLines(); | ||
|
||
// Part 1 - First we filter all patterns that can't be composited themselves. | ||
List<String> nonCompositablePatterns = [...towelPatterns]; | ||
for (String pattern in towelPatterns) { | ||
nonCompositablePatterns.remove(pattern); | ||
if (!_isDesignPossible('', pattern, nonCompositablePatterns)) nonCompositablePatterns.add(pattern); | ||
} | ||
|
||
// Now we check which designs are possible. | ||
int possibleDesigns = desiredDesigns.where((design) => _isDesignPossible('', design, nonCompositablePatterns)).length; | ||
|
||
return 'Possible designs: $possibleDesigns'; | ||
} | ||
|
||
bool _isDesignPossible(String currentPattern, String desiredPattern, List<String> towelPatterns) { | ||
for (String pattern in towelPatterns) { | ||
String testingPattern = '$currentPattern$pattern'; | ||
if (testingPattern == desiredPattern) { | ||
return true; | ||
} else if (testingPattern.length < desiredPattern.length && desiredPattern.startsWith(testingPattern)) { | ||
bool isPossible = _isDesignPossible(testingPattern, desiredPattern, towelPatterns); | ||
if (isPossible) return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
} |