rename kind type/variables

refactor schematic factory function
This commit is contained in:
onyx-and-iris 2024-12-27 00:39:46 +00:00
parent a282338f29
commit 51fed356af
3 changed files with 16 additions and 18 deletions

View File

@ -4,21 +4,21 @@ import (
"strings" "strings"
) )
type kindOfSchematic int type kind int
const ( const (
Lock kindOfSchematic = iota kindOfLock kind = iota
Key kindOfKey
) )
type schematic struct { type schematic struct {
kind kindOfSchematic kind kind
heights []int heights []int
data []string data []string
} }
func newSchematic(buf [][]byte) *schematic { func newSchematic(buf [][]byte) *schematic {
var kind kindOfSchematic var kind kind
data := make([]string, len(buf)) data := make([]string, len(buf))
heights := make([]int, len(buf[0])) heights := make([]int, len(buf[0]))
@ -26,17 +26,15 @@ func newSchematic(buf [][]byte) *schematic {
data[i] = string(line) data[i] = string(line)
if i == 0 { if i == 0 {
if allInString(data[i], '#') { switch {
kind = Lock case allInString(data[i], '#'):
} else if allInString(data[i], '.') { kind = kindOfLock
kind = Key case allInString(data[i], '.'):
kind = kindOfKey
} }
} }
if kind == Lock && i == 0 { if (kind == kindOfLock && i == 0) || (kind == kindOfKey && i == len(buf)-1) {
continue
}
if kind == Key && i == len(buf)-1 {
continue continue
} }

View File

@ -14,8 +14,8 @@ func Solve(buf []byte) (int, error) {
} }
var count int var count int
for _, lock := range schematics[Lock] { for _, lock := range schematics[kindOfLock] {
for _, key := range schematics[Key] { for _, key := range schematics[kindOfKey] {
if func() bool { if func() bool {
for i := range key.heights { for i := range key.heights {
if key.heights[i]+lock.heights[i] > maxHeight { if key.heights[i]+lock.heights[i] > maxHeight {

View File

@ -5,17 +5,17 @@ import (
"io" "io"
) )
func parseLines(r io.Reader) (int, map[kindOfSchematic][]*schematic, error) { func parseLines(r io.Reader) (int, map[kind][]*schematic, error) {
raw, _ := io.ReadAll(r) raw, _ := io.ReadAll(r)
rawSchematics := bytes.Split(raw, []byte("\n\n")) rawSchematics := bytes.Split(raw, []byte("\n\n"))
schematics := map[kindOfSchematic][]*schematic{Lock: {}, Key: {}} schematics := map[kind][]*schematic{kindOfLock: {}, kindOfKey: {}}
for _, rawSchematic := range rawSchematics { for _, rawSchematic := range rawSchematics {
s := newSchematic(bytes.Split(rawSchematic, []byte("\n"))) s := newSchematic(bytes.Split(rawSchematic, []byte("\n")))
schematics[s.kind] = append(schematics[s.kind], s) schematics[s.kind] = append(schematics[s.kind], s)
} }
maxHeight := len(schematics[Lock][0].heights) maxHeight := len(schematics[kindOfLock][0].heights)
return maxHeight, schematics, nil return maxHeight, schematics, nil
} }