Solution for 12.1.

This commit is contained in:
Anna Rose Wiggins 2018-12-12 18:20:33 -05:00
parent 2d71871cf4
commit fbd7c8fb00
No known key found for this signature in database
GPG key ID: 8D9ACA841015C59A
4 changed files with 163 additions and 0 deletions

View file

@ -0,0 +1,80 @@
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
}
}
}
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
}