mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-09 14:20:48 +00:00
rename kind type/variables
refactor schematic factory function
This commit is contained in:
parent
a282338f29
commit
51fed356af
@ -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
|
||||
}
|
||||
|
||||
|
@ -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 {
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user