From f430d4ed7b0470071c79cfb3987e8dab06d50580 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Thu, 26 Dec 2024 13:27:58 +0000 Subject: [PATCH] convert straight from bin to dec. --- day-24/internal/one/solve.go | 10 +++++----- day-24/internal/one/util.go | 8 -------- day-24/internal/one/zwire.go | 2 +- 3 files changed, 6 insertions(+), 14 deletions(-) diff --git a/day-24/internal/one/solve.go b/day-24/internal/one/solve.go index 5ed5883..b07eb09 100644 --- a/day-24/internal/one/solve.go +++ b/day-24/internal/one/solve.go @@ -2,8 +2,8 @@ package one import ( "bytes" + "math" "sort" - "strconv" "strings" ) @@ -20,12 +20,12 @@ func Solve(buf []byte) (int, error) { }) sort.Sort(byName(zWires)) - var binStr strings.Builder - for _, zWire := range zWires { - binStr.WriteString(strconv.Itoa(zWire.value)) + var decimal int + for i, zWire := range zWires { + decimal += zWire.value * int(math.Pow(float64(2), float64(i))) } - return mustConvBinToDec(binStr.String()), nil + return decimal, nil } func calculateWire(expressions []expression, wires map[string]int) map[string]int { diff --git a/day-24/internal/one/util.go b/day-24/internal/one/util.go index ddc4ab4..563e1cd 100644 --- a/day-24/internal/one/util.go +++ b/day-24/internal/one/util.go @@ -79,14 +79,6 @@ func anyNegative(wires map[string]int) bool { return false } -func mustConvBinToDec(s string) int { - n, err := strconv.ParseInt(s, 2, 64) - if err != nil { - panic(err) - } - return int(n) -} - func filter(wires map[string]int, fn func(string) bool) []zWire { var filtered []zWire for name, value := range wires { diff --git a/day-24/internal/one/zwire.go b/day-24/internal/one/zwire.go index bbdc6e2..39edc20 100644 --- a/day-24/internal/one/zwire.go +++ b/day-24/internal/one/zwire.go @@ -8,7 +8,7 @@ func (b byName) Len() int { return len(b) } func (b byName) Less(i, j int) bool { - return b[j].name < b[i].name + return b[j].name > b[i].name } func (b byName) Swap(i, j int) { b[i], b[j] = b[j], b[i]