aoc2024/day-25/internal/one/schematic.go
onyx-and-iris 51fed356af rename kind type/variables
refactor schematic factory function
2024-12-27 00:39:46 +00:00

54 lines
798 B
Go

package one
import (
"strings"
)
type kind int
const (
kindOfLock kind = iota
kindOfKey
)
type schematic struct {
kind kind
heights []int
data []string
}
func newSchematic(buf [][]byte) *schematic {
var kind kind
data := make([]string, len(buf))
heights := make([]int, len(buf[0]))
for i, line := range buf {
data[i] = string(line)
if i == 0 {
switch {
case allInString(data[i], '#'):
kind = kindOfLock
case allInString(data[i], '.'):
kind = kindOfKey
}
}
if (kind == kindOfLock && i == 0) || (kind == kindOfKey && 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")
}