mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-09 22:30:47 +00:00
refactor with filter() and byName()
This commit is contained in:
parent
4aaf594e23
commit
24bb0d40c9
@ -2,42 +2,28 @@ package one
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"cmp"
|
||||
"slices"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func Solve(buf []byte) (int, error) {
|
||||
r := bytes.NewReader(buf)
|
||||
initial, wires, expressions, err := parseLines(r)
|
||||
wires, expressions, err := parseLines(r)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
for k, v := range initial {
|
||||
wires[k] = v
|
||||
}
|
||||
|
||||
wires = calculateWire(expressions, wires)
|
||||
|
||||
zWires := []zWire{}
|
||||
for k, v := range wires {
|
||||
if strings.HasPrefix(k, "z") {
|
||||
zWires = append(zWires, zWire{k, v})
|
||||
}
|
||||
}
|
||||
slices.SortFunc(zWires, func(a, b zWire) int {
|
||||
return cmp.Compare(b.name, a.name)
|
||||
zWires := filter(wires, func(name string) bool {
|
||||
return strings.HasPrefix(name, "z")
|
||||
})
|
||||
sort.Sort(byName(zWires))
|
||||
|
||||
var binStr strings.Builder
|
||||
for _, zWire := range zWires {
|
||||
binStr.WriteString(strconv.FormatInt(int64(zWire.value), 2))
|
||||
binStr.WriteString(strconv.Itoa(zWire.value))
|
||||
}
|
||||
log.Debug(binStr.String())
|
||||
|
||||
return mustConvBinToDec(binStr.String()), nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user