From 51fed356af2c77616b5ca45fe639279c7cd6f680 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Fri, 27 Dec 2024 00:39:46 +0000 Subject: [PATCH] rename kind type/variables refactor schematic factory function --- day-25/internal/one/schematic.go | 24 +++++++++++------------- day-25/internal/one/solve.go | 4 ++-- day-25/internal/one/util.go | 6 +++--- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/day-25/internal/one/schematic.go b/day-25/internal/one/schematic.go index 89a47ac..2d9227b 100644 --- a/day-25/internal/one/schematic.go +++ b/day-25/internal/one/schematic.go @@ -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 } diff --git a/day-25/internal/one/solve.go b/day-25/internal/one/solve.go index 9f0f5f0..16885bb 100644 --- a/day-25/internal/one/solve.go +++ b/day-25/internal/one/solve.go @@ -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 { diff --git a/day-25/internal/one/util.go b/day-25/internal/one/util.go index 351f497..1bbf277 100644 --- a/day-25/internal/one/util.go +++ b/day-25/internal/one/util.go @@ -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 }