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. "24bb0d40c96ffa466143a8aacaa3238d5aa09f2b" and "5a6947af2ce94befb98892c8a0baa7227f068e05" have entirely different histories.
24bb0d40c9
...
5a6947af2c
@ -2,28 +2,42 @@ package one
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"sort"
|
"cmp"
|
||||||
|
"slices"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Solve(buf []byte) (int, error) {
|
func Solve(buf []byte) (int, error) {
|
||||||
r := bytes.NewReader(buf)
|
r := bytes.NewReader(buf)
|
||||||
wires, expressions, err := parseLines(r)
|
initial, wires, expressions, err := parseLines(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for k, v := range initial {
|
||||||
|
wires[k] = v
|
||||||
|
}
|
||||||
|
|
||||||
wires = calculateWire(expressions, wires)
|
wires = calculateWire(expressions, wires)
|
||||||
zWires := filter(wires, func(name string) bool {
|
|
||||||
return strings.HasPrefix(name, "z")
|
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)
|
||||||
})
|
})
|
||||||
sort.Sort(byName(zWires))
|
|
||||||
|
|
||||||
var binStr strings.Builder
|
var binStr strings.Builder
|
||||||
for _, zWire := range zWires {
|
for _, zWire := range zWires {
|
||||||
binStr.WriteString(strconv.Itoa(zWire.value))
|
binStr.WriteString(strconv.FormatInt(int64(zWire.value), 2))
|
||||||
}
|
}
|
||||||
|
log.Debug(binStr.String())
|
||||||
|
|
||||||
return mustConvBinToDec(binStr.String()), nil
|
return mustConvBinToDec(binStr.String()), nil
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@ const (
|
|||||||
target
|
target
|
||||||
)
|
)
|
||||||
|
|
||||||
func parseLines(r io.Reader) (map[string]int, []expression, error) {
|
func parseLines(r io.Reader) (map[string]int, map[string]int, []expression, error) {
|
||||||
initial := make(map[string]int)
|
initial := make(map[string]int)
|
||||||
wires := make(map[string]int)
|
wires := make(map[string]int)
|
||||||
expressions := []expression{}
|
expressions := []expression{}
|
||||||
@ -52,14 +52,10 @@ func parseLines(r io.Reader) (map[string]int, []expression, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := scanner.Err(); err != nil {
|
if err := scanner.Err(); err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for k, v := range initial {
|
return initial, wires, expressions, nil
|
||||||
wires[k] = v
|
|
||||||
}
|
|
||||||
|
|
||||||
return wires, expressions, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func mustConv(s string) int {
|
func mustConv(s string) int {
|
||||||
@ -86,13 +82,3 @@ func mustConvBinToDec(s string) int {
|
|||||||
}
|
}
|
||||||
return int(n)
|
return int(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
func filter(wires map[string]int, fn func(string) bool) []zWire {
|
|
||||||
var filtered []zWire
|
|
||||||
for name, value := range wires {
|
|
||||||
if fn(name) {
|
|
||||||
filtered = append(filtered, zWire{name, value})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return filtered
|
|
||||||
}
|
|
||||||
|
@ -2,18 +2,6 @@ package one
|
|||||||
|
|
||||||
const noValue = -1
|
const noValue = -1
|
||||||
|
|
||||||
type byName []zWire
|
|
||||||
|
|
||||||
func (b byName) Len() int {
|
|
||||||
return len(b)
|
|
||||||
}
|
|
||||||
func (b byName) Less(i, j int) bool {
|
|
||||||
return b[j].name < b[i].name
|
|
||||||
}
|
|
||||||
func (b byName) Swap(i, j int) {
|
|
||||||
b[i], b[j] = b[j], b[i]
|
|
||||||
}
|
|
||||||
|
|
||||||
type zWire struct {
|
type zWire struct {
|
||||||
name string
|
name string
|
||||||
value int
|
value int
|
||||||
|
Loading…
x
Reference in New Issue
Block a user