Metadata
-
Date
-
Tagged
-
Part of series
- Advent of Code 2024 Day 1
- Advent of Code 2024 Day 2
- Advent of Code 2024 Day 3
- Advent of Code 2024 Day 4
- Advent of Code 2024 Day 5
- Advent of Code 2024 Day 6
- Advent of Code 2024 Day 7
- Advent of Code 2024 Day 8
- Advent of Code 2024 Day 9
- Advent of Code 2024 Day 10
- Advent of Code 2024 Day 11
- Advent of Code 2024 Day 12
- Advent of Code 2024 Day 13
- Advent of Code 2024 Day 14
- Advent of Code 2024 Day 15
- Advent of Code 2024 Day 16
- Advent of Code 2024 Day 17
- Advent of Code 2024 Day 18
- Advent of Code 2024 Day 19
- Advent of Code 2024 Day 20
- Advent of Code 2024 Day 21
- Advent of Code 2024 Day 22
- Advent of Code 2024 Day 23
- Advent of Code 2024 Day 24
- Advent of Code 2024 Day 25
-
Older post
-
Newer post
Advent of Code 2024 Day 19
Day 19: Linen Layout
https://adventofcode.com/2024/day/19
You are at a spa and have to arrange a bunch of towels into patterns.
An example input looks like this:
The top section is a list of available towel types, the bottom section is a list of patterns to make with those towels.
For each towel type, the spa has infinitely many of them, they want to know if it’s possible to combine them in ways that make a certain pattern.
What the letters in the input mean is not really important, but here goes anyway: each letter in a towel represents a coloured stripe. The patterns have a bunch of coloured stripes in a specific order.
Flipping a towel to get its pattern in reverse in not allowed.
Parsing
Making 2 lists of strings.
Part 1
The question asks how many patterns are possible to make given the available towel types.
Yay, this is a recursive function!
Count the amount of ways a pattern can be made, inside the function that does that count the amount of ways a smaller pattern can be made.
First some skeleton/pseudo-code for the thing I want to code towards.
Helpers
The recursive function.
It takes a pattern I want to make, and a set of available towel types. For each available type of towel, I check if the wanted pattern starts with the stripes on that towel. If it does, I use that same function to count how many ways there are to make the pattern with that towel cut off from the start of the pattern. I then return the sum of all those ways.
The base case for my resursion is when the pattern is empty, because then we successfully cut off a bunch of towels that make the original pattern.
Code
Now, that code is correct, but slow. It recomputes the same thing over and over.
So I remember previous work and store it in a cache.
Feeding that cache into my recursive function and BAM, I just did some dynamic programming.
Part 2
The question asks for the sum of the amount of ways the patterns can be made.
My part1 paid off, and part2 is a tiny change now!