adventofcode/2018/internal/day12/plants.go

82 lines
1.3 KiB
Go

package day12
type Rule struct {
Positions [5]bool
Result bool
}
func (r Rule) String() string {
res := ""
for _, p := range r.Positions {
res += plantString(p)
}
res += " => "
res += plantString(r.Result)
return res
}
func plantString(isPlant bool) string {
if isPlant {
return "#"
}
return "."
}
func getPlantBounds(plants map[int]bool) (int, int) {
keys := make([]int, len(plants))
i := 0
for key, _ := range plants {
keys[i] = key
i++
}
low := keys[0]
high := keys[0]
for _, key := range keys {
if key < low {
low = key
}
if key > high {
high = key
}
}
return low, high
}
func RunGeneration(plants map[int]bool, rules []*Rule) map[int]bool {
newGen := make(map[int]bool)
low, high := getPlantBounds(plants)
for i := low - 2; i < high+3; i++ {
for _, rule := range rules {
if checkRule(plants, rule, i) {
newGen[i] = rule.Result
break // only one rule will match a given slice
}
}
}
return newGen
}
func checkRule(plants map[int]bool, rule *Rule, i int) bool {
for j := 0; j < 5; j++ {
if plants[i+j-2] != rule.Positions[j] {
return false
}
}
return true
}
func SumPlants(plants map[int]bool) int {
sum := 0
for key, value := range plants {
if value {
sum += key
}
}
return sum
}