Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solving day 19 #23

Merged
merged 1 commit into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions docs/day19.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
url: "https://adventofcode.com/2024/day/19"
---

## Day 19: Linen Layout

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.

Could this finally be your chance to visit the onsen next door? Only one way to find out.

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!

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.)

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).

To start, collect together all of the available towel patterns and the list of desired designs (your puzzle input). For example:

```txt
r, wr, b, g, bwu, rb, gb, br

brwrr
bggr
gbbr
rrbgbr
ubwu
bwurrg
brgr
bbrgwb
```

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.

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.

Not all designs will be possible with the available towels. In the above example, the designs are possible or impossible as follows:

* `brwrr` can be made with a `br` towel, then a `wr` towel, and then finally an `r` towel.
* `bggr` can be made with a `b` towel, two `g` towels, and then an `r` towel.
* `gbbr` can be made with a `gb` towel and then a `br` towel.
* `rrbgbr` can be made with `r`, `rb`, `g`, and `br`.
* `ubwu` is impossible.
* `bwurrg` can be made with `bwu`, `r`, `r`, and `g`.
* `brgr` can be made with `br`, `g`, and `r`.
* `bbrgwb` is impossible.

In this example, `6` of the eight designs are possible with the available towel patterns.

To get into the onsen as soon as possible, consult your list of towel patterns and desired designs carefully. How many designs are possible?
95 changes: 95 additions & 0 deletions src/day19/day19.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package day19

import (
"slices"
"strings"
)

const THREAD_POOL = 32

type trie struct {
chr rune
next map[rune]*trie
end bool
}

type thread struct {
next map[rune]*trie
pattern string
}

func Solve(input string) uint {
a, patterns := parseInput(input)
t := buildPossibilities(a)
var cnt uint = 0
for _, p := range patterns {
if isPatternPossible(t.next, p) {
cnt++
}
}
return cnt
}

func parseInput(input string) ([]string, []string) {
lines := strings.Split(input, "\n")
return strings.Split(lines[0], ", "), lines[2:]
}

func buildPossibilities(towels []string) trie {
t := trie{next: make(map[rune]*trie)}
for _, pattern := range towels {
var prevT *trie = &t
for i, r := range pattern {
if _, ok := prevT.next[r]; !ok {
tmp := trie{chr: r, next: make(map[rune]*trie)}
prevT.next[r] = &tmp
}
prevT = prevT.next[r]
if i == len(pattern)-1 {
prevT.end = true
}
}
}
return t
}

func isPatternPossible(possibilities map[rune]*trie, pattern string) bool {
threads := []thread{{next: possibilities, pattern: pattern}}
coldStorage := make([]thread, 0)
for {
if len(threads) == 0 {
if len(coldStorage) > 0 {
x := THREAD_POOL
if len(coldStorage) < x {
x = len(coldStorage)
}
threads = coldStorage[:x]
coldStorage = slices.Delete(coldStorage, 0, x)
} else {
return false
}
}

next := make([]thread, 0)
for i, t := range threads {
if i > THREAD_POOL {
coldStorage = append(coldStorage, t)
continue
}

if v, ok := t.next[rune(t.pattern[0])]; ok {
if len(t.pattern) == 1 {
return true
}

thr := thread{pattern: t.pattern[1:], next: v.next}
next = append([]thread{thr}, next...)
if v.end {
thr := thread{pattern: t.pattern[1:], next: possibilities}
next = append([]thread{thr}, next...)
}
}
}
threads = next
}
}
Loading
Loading