Skip to content

Commit 6e59d79

Browse files
committed
add 2024/19-20
1 parent 3522b07 commit 6e59d79

12 files changed

+902
-1
lines changed

2024/19/README.md

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# 2024, Day 19: Linen Layout
2+
3+
Today, The Historians take you up to the [hot springs](../../2023/12) on Gear Island! Very [suspiciously](https://www.youtube.com/watch?v=ekL881PJMjI), absolutely nothing goes wrong as they begin their careful search of the vast field of helixes.
4+
5+
Could this _finally_ be your chance to visit the [onsen](https://en.wikipedia.org/wiki/Onsen) next door? Only one way to find out.
6+
7+
After a brief conversation with the reception staff at the onsen front desk, you discover that you don't have the right kind of money to pay the admission fee. However, before you can leave, the staff get your attention. Apparently, they've heard about how you helped at the hot springs, and they're willing to make a deal: if you can simply help them _arrange their towels_, they'll let you in for _free_!
8+
9+
## Part 1
10+
11+
Every towel at this onsen is marked with a _pattern of colored stripes_. There are only a few patterns, but for any particular pattern, the staff can get you as many towels with that pattern as you need. Each stripe can be _white_ (`w`), _blue_ (`u`), _black_ (`b`), _red_ (`r`), or _green_ (`g`). So, a towel with the pattern `ggr` would have a green stripe, a green stripe, and then a red stripe, in that order. (You can't reverse a pattern by flipping a towel upside-down, as that would cause the onsen logo to face the wrong way.)
12+
13+
The Official Onsen Branding Expert has produced a list of _designs_ - each a long sequence of stripe colors - that they would like to be able to display. You can use any towels you want, but all of the towels' stripes must exactly match the desired design. So, to display the design `rgrgr`, you could use two `rg` towels and then an `r` towel, an `rgr` towel and then a `gr` towel, or even a single massive `rgrgr` towel (assuming such towel patterns were actually available).
14+
15+
To start, collect together all of the available towel patterns and the list of desired designs (your puzzle input). For example:
16+
17+
r, wr, b, g, bwu, rb, gb, br
18+
19+
brwrr
20+
bggr
21+
gbbr
22+
rrbgbr
23+
ubwu
24+
bwurrg
25+
brgr
26+
bbrgwb
27+
28+
The first line indicates the available towel patterns; in this example, the onsen has unlimited towels with a single red stripe (`r`), unlimited towels with a white stripe and then a red stripe (`wr`), and so on.
29+
30+
After the blank line, the remaining lines each describe a design the onsen would like to be able to display. In this example, the first design (`brwrr`) indicates that the onsen would like to be able to display a black stripe, a red stripe, a white stripe, and then two red stripes, in that order.
31+
32+
Not all designs will be possible with the available towels. In the above example, the designs are possible or impossible as follows:
33+
34+
* `brwrr` can be made with a `br` towel, then a `wr` towel, and then finally an `r` towel.
35+
* `bggr` can be made with a `b` towel, two `g` towels, and then an `r` towel.
36+
* `gbbr` can be made with a `gb` towel and then a `br` towel.
37+
* `rrbgbr` can be made with `r`, `rb`, `g`, and `br`.
38+
* `ubwu` is _impossible_.
39+
* `bwurrg` can be made with `bwu`, `r`, `r`, and `g`.
40+
* `brgr` can be made with `br`, `g`, and `r`.
41+
* `bbrgwb` is _impossible_.
42+
43+
In this example, _`6`_ of the eight designs are possible with the available towel patterns.
44+
45+
To get into the onsen as soon as possible, consult your list of towel patterns and desired designs carefully. _How many designs are possible?_
46+
47+
Your puzzle answer was `265`.
48+
49+
## Part 2
50+
51+
The staff don't really like some of the towel arrangements you came up with. To avoid an endless cycle of towel rearrangement, maybe you should just give them every possible option.
52+
53+
Here are all of the different ways the above example's designs can be made:
54+
55+
`brwrr` can be made in two different ways: `b`, `r`, `wr`, `r` _or_ `br`, `wr`, `r`.
56+
57+
`bggr` can only be made with `b`, `g`, `g`, and `r`.
58+
59+
`gbbr` can be made 4 different ways:
60+
61+
* `g`, `b`, `b`, `r`
62+
* `g`, `b`, `br`
63+
* `gb`, `b`, `r`
64+
* `gb`, `br`
65+
66+
`rrbgbr` can be made 6 different ways:
67+
68+
* `r`, `r`, `b`, `g`, `b`, `r`
69+
* `r`, `r`, `b`, `g`, `br`
70+
* `r`, `r`, `b`, `gb`, `r`
71+
* `r`, `rb`, `g`, `b`, `r`
72+
* `r`, `rb`, `g`, `br`
73+
* `r`, `rb`, `gb`, `r`
74+
75+
`bwurrg` can only be made with `bwu`, `r`, `r`, and `g`.
76+
77+
`brgr` can be made in two different ways: `b`, `r`, `g`, `r` _or_ `br`, `g`, `r`.
78+
79+
`ubwu` and `bbrgwb` are still impossible.
80+
81+
Adding up all of the ways the towels in this example could be arranged into the desired designs yields _`16`_ (`2 + 1 + 4 + 6 + 1 + 2`).
82+
83+
They'll let you into the onsen as soon as you have the list. _What do you get if you add up the number of different ways you could make each design?_
84+
85+
Your puzzle answer was `752461716635602`.
86+
87+
## Solution Notes
88+
89+
Part 1's task can be done directly with regular expressions, so unsurprisingly, the best solution for that is to turn the input pattern list into a regex of form `/(r|wr|b|g|...|br)+$/` and check if that matches.
90+
91+
For part 2, the task of the regular expression matcher needs to be spelled out "by hand" in the form of a rather basic DFS with memoization.
92+
93+
Fun fact: Part 2's solution can be turned into part 1 simply by replacing the `abs()` call with `any()`.
94+
95+
* Part 1, Python: 125 bytes, <100 ms
96+
* Part 2, Python: 206 bytes, ~500 ms

2024/19/aoc2024_19_part1.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import re;k=0
2+
for l in open("input.txt"):
3+
if','in l:R="("+l.replace(',','|')+r")+\s*$"
4+
if re.match(R,l,re.X):k+=1
5+
print(k)

2024/19/aoc2024_19_part2.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import functools as F
2+
@F.cache
3+
def D(x):return sum(D(x[len(z):])for z in P if x[:len(z)]==z)if x else 1
4+
S,n=str.strip,-1
5+
for l in open("input.txt"):
6+
if','in l:P=[*map(S,l.split(','))]
7+
n+=D(S(l))
8+
print(n)

0 commit comments

Comments
 (0)