Skip to content

Commit 34e5851

Browse files
committed
feat: Solving day 19
1 parent 733219b commit 34e5851

File tree

3 files changed

+585
-0
lines changed

3 files changed

+585
-0
lines changed

docs/day19.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
url: "https://adventofcode.com/2024/day/19"
3+
---
4+
5+
## Day 19: Linen Layout
6+
7+
Today, The Historians take you up to the hot springs on Gear Island! Very suspiciously, absolutely nothing goes wrong as they begin their careful search of the vast field of helixes.
8+
9+
Could this finally be your chance to visit the onsen next door? Only one way to find out.
10+
11+
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!
12+
13+
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.)
14+
15+
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).
16+
17+
To start, collect together all of the available towel patterns and the list of desired designs (your puzzle input). For example:
18+
19+
```txt
20+
r, wr, b, g, bwu, rb, gb, br
21+
22+
brwrr
23+
bggr
24+
gbbr
25+
rrbgbr
26+
ubwu
27+
bwurrg
28+
brgr
29+
bbrgwb
30+
```
31+
32+
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.
33+
34+
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.
35+
36+
Not all designs will be possible with the available towels. In the above example, the designs are possible or impossible as follows:
37+
38+
* `brwrr` can be made with a `br` towel, then a `wr` towel, and then finally an `r` towel.
39+
* `bggr` can be made with a `b` towel, two `g` towels, and then an `r` towel.
40+
* `gbbr` can be made with a `gb` towel and then a `br` towel.
41+
* `rrbgbr` can be made with `r`, `rb`, `g`, and `br`.
42+
* `ubwu` is impossible.
43+
* `bwurrg` can be made with `bwu`, `r`, `r`, and `g`.
44+
* `brgr` can be made with `br`, `g`, and `r`.
45+
* `bbrgwb` is impossible.
46+
47+
In this example, `6` of the eight designs are possible with the available towel patterns.
48+
49+
To get into the onsen as soon as possible, consult your list of towel patterns and desired designs carefully. How many designs are possible?

src/day19/day19.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package day19
2+
3+
import (
4+
"slices"
5+
"strings"
6+
)
7+
8+
const THREAD_POOL = 32
9+
10+
type trie struct {
11+
chr rune
12+
next map[rune]*trie
13+
end bool
14+
}
15+
16+
type thread struct {
17+
next map[rune]*trie
18+
pattern string
19+
}
20+
21+
func Solve(input string) uint {
22+
a, patterns := parseInput(input)
23+
t := buildPossibilities(a)
24+
var cnt uint = 0
25+
for _, p := range patterns {
26+
if isPatternPossible(t.next, p) {
27+
cnt++
28+
}
29+
}
30+
return cnt
31+
}
32+
33+
func parseInput(input string) ([]string, []string) {
34+
lines := strings.Split(input, "\n")
35+
return strings.Split(lines[0], ", "), lines[2:]
36+
}
37+
38+
func buildPossibilities(towels []string) trie {
39+
t := trie{next: make(map[rune]*trie)}
40+
for _, pattern := range towels {
41+
var prevT *trie = &t
42+
for i, r := range pattern {
43+
if _, ok := prevT.next[r]; !ok {
44+
tmp := trie{chr: r, next: make(map[rune]*trie)}
45+
prevT.next[r] = &tmp
46+
}
47+
prevT = prevT.next[r]
48+
if i == len(pattern)-1 {
49+
prevT.end = true
50+
}
51+
}
52+
}
53+
return t
54+
}
55+
56+
func isPatternPossible(possibilities map[rune]*trie, pattern string) bool {
57+
threads := []thread{{next: possibilities, pattern: pattern}}
58+
coldStorage := make([]thread, 0)
59+
for {
60+
if len(threads) == 0 {
61+
if len(coldStorage) > 0 {
62+
x := THREAD_POOL
63+
if len(coldStorage) < x {
64+
x = len(coldStorage)
65+
}
66+
threads = coldStorage[:x]
67+
coldStorage = slices.Delete(coldStorage, 0, x)
68+
} else {
69+
return false
70+
}
71+
}
72+
73+
next := make([]thread, 0)
74+
for i, t := range threads {
75+
if i > THREAD_POOL {
76+
coldStorage = append(coldStorage, t)
77+
continue
78+
}
79+
80+
if v, ok := t.next[rune(t.pattern[0])]; ok {
81+
if len(t.pattern) == 1 {
82+
return true
83+
}
84+
85+
thr := thread{pattern: t.pattern[1:], next: v.next}
86+
next = append([]thread{thr}, next...)
87+
if v.end {
88+
thr := thread{pattern: t.pattern[1:], next: possibilities}
89+
next = append([]thread{thr}, next...)
90+
}
91+
}
92+
}
93+
threads = next
94+
}
95+
}

0 commit comments

Comments
 (0)