mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-04-19 19:23:47 +01:00
Compare commits
No commits in common. "942d2a84565d538d759da0d68cb80e99d5c25b2b" and "24bb0d40c96ffa466143a8aacaa3238d5aa09f2b" have entirely different histories.
942d2a8456
...
24bb0d40c9
@ -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 decimal int
|
var binStr strings.Builder
|
||||||
for i, zWire := range zWires {
|
for _, zWire := range zWires {
|
||||||
decimal += zWire.value * int(math.Pow(float64(2), float64(i)))
|
binStr.WriteString(strconv.Itoa(zWire.value))
|
||||||
}
|
}
|
||||||
|
|
||||||
return decimal, nil
|
return mustConvBinToDec(binStr.String()), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func calculateWire(expressions []expression, wires map[string]int) map[string]int {
|
func calculateWire(expressions []expression, wires map[string]int) map[string]int {
|
||||||
|
@ -79,6 +79,14 @@ 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 {
|
||||||
|
@ -7,11 +7,9 @@ type byName []zWire
|
|||||||
func (b byName) Len() int {
|
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[i].name < b[j].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]
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user