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

View File

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

View File

@ -5,17 +5,17 @@ import (
"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)
rawSchematics := bytes.Split(raw, []byte("\n\n"))
schematics := map[kindOfSchematic][]*schematic{Lock: {}, Key: {}}
schematics := map[kind][]*schematic{kindOfLock: {}, kindOfKey: {}}
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)
maxHeight := len(schematics[kindOfLock][0].heights)
return maxHeight, schematics, nil
}