mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2026-04-09 02:23:36 +00:00
add day-25 part1
This commit is contained in:
55
day-25/internal/one/schematic.go
Normal file
55
day-25/internal/one/schematic.go
Normal 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")
|
||||
}
|
||||
34
day-25/internal/one/solve.go
Normal file
34
day-25/internal/one/solve.go
Normal 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
|
||||
}
|
||||
15
day-25/internal/one/solve_internal_test.go
Normal file
15
day-25/internal/one/solve_internal_test.go
Normal 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)
|
||||
}
|
||||
30
day-25/internal/one/util.go
Normal file
30
day-25/internal/one/util.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user