From 2780eb7f831de5f655ed69d954a1447b256c271d Mon Sep 17 00:00:00 2001 From: Sander in 't Hout Date: Fri, 20 Dec 2024 01:13:59 +0100 Subject: [PATCH] Add AoC 2024 day 19 part 1 solver --- assets/advent_of_code/2024_19_sample.txt | 10 +++++ .../advent_of_code/2024/_all_solvers.dart | 1 + .../advent_of_code/2024/day_19_solver.dart | 44 +++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 assets/advent_of_code/2024_19_sample.txt create mode 100644 lib/solvers/advent_of_code/2024/day_19_solver.dart diff --git a/assets/advent_of_code/2024_19_sample.txt b/assets/advent_of_code/2024_19_sample.txt new file mode 100644 index 0000000..ad43a74 --- /dev/null +++ b/assets/advent_of_code/2024_19_sample.txt @@ -0,0 +1,10 @@ +r, wr, b, g, bwu, rb, gb, br + +brwrr +bggr +gbbr +rrbgbr +ubwu +bwurrg +brgr +bbrgwb \ No newline at end of file diff --git a/lib/solvers/advent_of_code/2024/_all_solvers.dart b/lib/solvers/advent_of_code/2024/_all_solvers.dart index 1bdb038..324a720 100644 --- a/lib/solvers/advent_of_code/2024/_all_solvers.dart +++ b/lib/solvers/advent_of_code/2024/_all_solvers.dart @@ -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'; diff --git a/lib/solvers/advent_of_code/2024/day_19_solver.dart b/lib/solvers/advent_of_code/2024/day_19_solver.dart new file mode 100644 index 0000000..d093c66 --- /dev/null +++ b/lib/solvers/advent_of_code/2024/day_19_solver.dart @@ -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 inputParts = input.split(splitNewline); + + List towelPatterns = inputParts[0].split(', ').toList(); + List desiredDesigns = inputParts[1].toListOfLines(); + + // Part 1 - First we filter all patterns that can't be composited themselves. + List 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 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; + } + +}