add day-19 + benchmarks

This commit is contained in:
2024-12-19 09:39:38 +00:00
parent f1cac7da7b
commit c3fa65e4a8
15 changed files with 381 additions and 0 deletions

View File

@@ -0,0 +1,6 @@
goos: linux
goarch: amd64
pkg: github.com/onyx-and-iris/aoc2024/day-19/internal/one
cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
BenchmarkSolve-12 1000000000 0.1706 ns/op
ok github.com/onyx-and-iris/aoc2024/day-19/internal/one 2.018s

View File

@@ -0,0 +1,58 @@
package one
import (
"bytes"
"cmp"
"slices"
"strings"
log "github.com/sirupsen/logrus"
)
func Solve(buf []byte) (int, error) {
r := bytes.NewReader(buf)
stripes, designs, err := parseLines(r)
if err != nil {
return 0, err
}
log.Debug(stripes, designs)
var sum int
for _, design := range designs {
ok := isPossibleArrangement(design, stripes)
if ok {
log.Debugf("design: %s is possible", design)
sum++
}
}
return sum, nil
}
func isPossibleArrangement(design string, stripes []string) bool {
if len(design) == 0 {
return true
}
greedy := []string{}
for _, stripe := range stripes {
log.Debugf("testing design: %s with stripe: %s", design, stripe)
if strings.HasPrefix(design, stripe) {
log.Debugf("design: %s has prefix %s", design, stripe)
greedy = append(greedy, stripe)
}
}
slices.SortFunc(greedy, func(a, b string) int {
return cmp.Compare(len(b), len(a))
})
for _, g := range greedy {
if isPossibleArrangement(design[len(g):], stripes) {
return true
}
}
return false
}

View File

@@ -0,0 +1,15 @@
package one
import (
_ "embed"
"os"
"testing"
)
//go:embed testdata/input.txt
var data []byte
func BenchmarkSolve(b *testing.B) {
os.Stdout, _ = os.Open(os.DevNull)
Solve(data)
}

View File

@@ -0,0 +1,33 @@
package one
import (
"bufio"
"io"
"strings"
)
func parseLines(r io.Reader) ([]string, []string, error) {
var stripes, designs []string
var inDesigns bool
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := scanner.Text()
if line == "" {
inDesigns = true
continue
}
if inDesigns {
designs = append(designs, line)
} else {
stripes = strings.Split(line, ", ")
}
}
if err := scanner.Err(); err != nil {
return nil, nil, err
}
return stripes, designs, nil
}

View File

@@ -0,0 +1,6 @@
goos: linux
goarch: amd64
pkg: github.com/onyx-and-iris/aoc2024/day-19/internal/two
cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
BenchmarkSolve-12 1000000000 0.01208 ns/op
ok github.com/onyx-and-iris/aoc2024/day-19/internal/two 0.091s

View File

@@ -0,0 +1,72 @@
package two
import (
"bytes"
"cmp"
"slices"
"strings"
log "github.com/sirupsen/logrus"
)
func Solve(buf []byte) (int, error) {
r := bytes.NewReader(buf)
stripes, designs, err := parseLines(r)
if err != nil {
return 0, err
}
conc := len(designs)
countChan := make(chan int)
for _, design := range designs {
go func() {
count, ok := numPossibleArrangement(design, stripes, make(map[string]int))
if ok {
log.Debugf("design: %s is possible in %d ways", design, count)
countChan <- count
return
}
countChan <- 0
}()
}
var totalCount int
for range conc {
count := <-countChan
totalCount += count
}
return totalCount, nil
}
func numPossibleArrangement(design string, stripes []string, memo map[string]int) (int, bool) {
if len(design) == 0 {
return 1, true
}
greedy := []string{}
for _, stripe := range stripes {
if strings.HasPrefix(design, stripe) {
greedy = append(greedy, stripe)
}
}
slices.SortFunc(greedy, func(a, b string) int {
return cmp.Compare(len(b), len(a))
})
var totalCount int
for _, g := range greedy {
if count, ok := memo[design[len(g):]]; ok {
totalCount += count
continue
}
count, ok := numPossibleArrangement(design[len(g):], stripes, memo)
if ok {
totalCount += count
}
memo[design[len(g):]] = count
}
return totalCount, true
}

View File

@@ -0,0 +1,15 @@
package two
import (
_ "embed"
"os"
"testing"
)
//go:embed testdata/input.txt
var data []byte
func BenchmarkSolve(b *testing.B) {
os.Stdout, _ = os.Open(os.DevNull)
Solve(data)
}

View File

@@ -0,0 +1,33 @@
package two
import (
"bufio"
"io"
"strings"
)
func parseLines(r io.Reader) ([]string, []string, error) {
var stripes, designs []string
var inDesigns bool
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := scanner.Text()
if line == "" {
inDesigns = true
continue
}
if inDesigns {
designs = append(designs, line)
} else {
stripes = strings.Split(line, ", ")
}
}
if err := scanner.Err(); err != nil {
return nil, nil, err
}
return stripes, designs, nil
}