Solution for 12.1.
This commit is contained in:
parent
2d71871cf4
commit
fbd7c8fb00
4 changed files with 163 additions and 0 deletions
80
2018/internal/day12/plants.go
Normal file
80
2018/internal/day12/plants.go
Normal 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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue