Skip to content

Commit

Permalink
Add AoC 2024 day 19 part 1 solver
Browse files Browse the repository at this point in the history
  • Loading branch information
h3x4d3c1m4l committed Dec 20, 2024
1 parent 383544f commit 2780eb7
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
10 changes: 10 additions & 0 deletions assets/advent_of_code/2024_19_sample.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
1 change: 1 addition & 0 deletions lib/solvers/advent_of_code/2024/_all_solvers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export 'day_13_solver.dart';
export 'day_14_solver.dart';
export 'day_15_solver.dart';
export 'day_17_solver.dart';
export 'day_19_solver.dart';
44 changes: 44 additions & 0 deletions lib/solvers/advent_of_code/2024/day_19_solver.dart
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;
}

}

0 comments on commit 2780eb7

Please sign in to comment.