convert straight from bin to dec.

This commit is contained in:
onyx-and-iris 2024-12-26 13:27:58 +00:00
parent 24bb0d40c9
commit f430d4ed7b
3 changed files with 6 additions and 14 deletions

View File

@ -2,8 +2,8 @@ package one
import ( import (
"bytes" "bytes"
"math"
"sort" "sort"
"strconv"
"strings" "strings"
) )
@ -20,12 +20,12 @@ func Solve(buf []byte) (int, error) {
}) })
sort.Sort(byName(zWires)) sort.Sort(byName(zWires))
var binStr strings.Builder var decimal int
for _, zWire := range zWires { for i, zWire := range zWires {
binStr.WriteString(strconv.Itoa(zWire.value)) 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 { func calculateWire(expressions []expression, wires map[string]int) map[string]int {

View File

@ -79,14 +79,6 @@ func anyNegative(wires map[string]int) bool {
return false 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 { func filter(wires map[string]int, fn func(string) bool) []zWire {
var filtered []zWire var filtered []zWire
for name, value := range wires { for name, value := range wires {

View File

@ -8,7 +8,7 @@ func (b byName) Len() int {
return len(b) return len(b)
} }
func (b byName) Less(i, j int) bool { 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) { func (b byName) Swap(i, j int) {
b[i], b[j] = b[j], b[i] b[i], b[j] = b[j], b[i]