add day-25 part1

This commit is contained in:
2024-12-26 19:00:37 +00:00
parent 942d2a8456
commit dda597a577
11 changed files with 264 additions and 0 deletions

View File

@@ -0,0 +1,55 @@
package one
import (
"strings"
)
type kindOfSchematic int
const (
Lock kindOfSchematic = iota
Key
)
type schematic struct {
kind kindOfSchematic
heights []int
data []string
}
func newSchematic(buf [][]byte) *schematic {
var kind kindOfSchematic
data := make([]string, len(buf))
heights := make([]int, len(buf[0]))
for i, line := range buf {
data[i] = string(line)
if i == 0 {
if allInString(data[i], '#') {
kind = Lock
} else if allInString(data[i], '.') {
kind = Key
}
}
if kind == Lock && i == 0 {
continue
}
if kind == Key && i == len(buf)-1 {
continue
}
for j, r := range data[i] {
if r == '#' {
heights[j]++
}
}
}
return &schematic{kind, heights, data}
}
func (s *schematic) String() string {
return strings.Join(s.data, "\n")
}

View File

@@ -0,0 +1,34 @@
package one
import (
"bytes"
log "github.com/sirupsen/logrus"
)
func Solve(buf []byte) (int, error) {
r := bytes.NewReader(buf)
maxHeight, schematics, err := parseLines(r)
if err != nil {
return 0, err
}
var count int
for _, lock := range schematics[Lock] {
for _, key := range schematics[Key] {
if func() bool {
for i := range key.heights {
if key.heights[i]+lock.heights[i] > maxHeight {
return false
}
}
return true
}() {
count++
log.Debugf("lock: %v and key: %v", lock.heights, key.heights)
}
}
}
return count, nil
}

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,30 @@
package one
import (
"bytes"
"io"
)
func parseLines(r io.Reader) (int, map[kindOfSchematic][]*schematic, error) {
raw, _ := io.ReadAll(r)
rawSchematics := bytes.Split(raw, []byte("\n\n"))
schematics := map[kindOfSchematic][]*schematic{Lock: {}, Key: {}}
for _, rawSchematic := range rawSchematics {
s := newSchematic(bytes.Split(rawSchematic, []byte("\n")))
schematics[s.kind] = append(schematics[s.kind], s)
}
maxHeight := len(schematics[Lock][0].heights)
return maxHeight, schematics, nil
}
func allInString(s string, r rune) bool {
for _, c := range s {
if c != r {
return false
}
}
return true
}