mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-09 22:30:47 +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"
|
"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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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 {
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user