61 lines
1.2 KiB
Go
61 lines
1.2 KiB
Go
package day12
|
|
|
|
import (
|
|
"strings"
|
|
)
|
|
|
|
func ParseInput(data []string) (map[int]bool, []*Rule) {
|
|
stateStrings := strings.Split(data[0], " ")
|
|
state := stateStrings[len(stateStrings)-1]
|
|
ruleStrings := data[2:]
|
|
|
|
// Populate plant map
|
|
plants := make(map[int]bool)
|
|
for i, byte := range []byte(state) {
|
|
if byte == '#' {
|
|
plants[i] = true
|
|
}
|
|
}
|
|
|
|
// Create rules
|
|
rules := []*Rule{}
|
|
for _, ruleStr := range ruleStrings {
|
|
ruleData := strings.Split(ruleStr, " ")
|
|
|
|
rule := &Rule{}
|
|
positions := []byte(ruleData[0])
|
|
|
|
for i := 0; i < 5; i++ {
|
|
rule.Positions[i] = positions[i] == '#'
|
|
}
|
|
|
|
if ruleData[2] == "#" {
|
|
rule.Result = true
|
|
} else {
|
|
rule.Result = false
|
|
}
|
|
|
|
rules = append(rules, rule)
|
|
}
|
|
|
|
return plants, rules
|
|
}
|
|
|
|
// The function isn't fast, the resulting data is.
|
|
func FastParseInput(data []string) ([]byte, map[string]byte) {
|
|
stateStrings := strings.Split(data[0], " ")
|
|
ruleStrings := data[2:]
|
|
|
|
// Populate plant list
|
|
plants := []byte(stateStrings[len(stateStrings)-1])
|
|
|
|
// Create rules
|
|
rules := make(map[string]byte)
|
|
for _, ruleStr := range ruleStrings {
|
|
ruleData := strings.Split(ruleStr, " ")
|
|
rules[ruleData[0]] = []byte(ruleData[2])[0]
|
|
}
|
|
|
|
return plants, rules
|
|
}
|