aoc2024/day-25/internal/one/schematic.go

54 lines
798 B
Go
Raw Normal View History

2024-12-26 19:00:37 +00:00
package one
import (
"strings"
)
type kind int
2024-12-26 19:00:37 +00:00
const (
kindOfLock kind = iota
kindOfKey
2024-12-26 19:00:37 +00:00
)
type schematic struct {
kind kind
2024-12-26 19:00:37 +00:00
heights []int
data []string
}
func newSchematic(buf [][]byte) *schematic {
var kind kind
2024-12-26 19:00:37 +00:00
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
2024-12-26 19:00:37 +00:00
}
}
if (kind == kindOfLock && i == 0) || (kind == kindOfKey && i == len(buf)-1) {
2024-12-26 19:00:37 +00:00
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")
}