99 lines
1.7 KiB
Go
99 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"git.annabunch.es/annabunches/adventofcode/2020/lib/util"
|
|
)
|
|
|
|
func parseInput(input []string) map[[2]int]bool {
|
|
tiles := make(map[[2]int]bool)
|
|
for _, line := range input {
|
|
coords := [2]int{0, 0}
|
|
for i := 0; i < len(line); i++ {
|
|
switch line[i] {
|
|
case 'e':
|
|
coords[0]++
|
|
case 'w':
|
|
coords[0]--
|
|
case 'n':
|
|
next := line[i+1]
|
|
switch next {
|
|
case 'e':
|
|
coords[0]++
|
|
coords[1]--
|
|
case 'w':
|
|
coords[1]--
|
|
}
|
|
i++
|
|
case 's':
|
|
next := line[i+1]
|
|
switch next {
|
|
case 'e':
|
|
coords[1]++
|
|
case 'w':
|
|
coords[0]--
|
|
coords[1]++
|
|
}
|
|
i++
|
|
}
|
|
}
|
|
if tile, ok := tiles[coords]; ok && tile == true {
|
|
tiles[coords] = false
|
|
} else {
|
|
tiles[coords] = true
|
|
}
|
|
}
|
|
|
|
return tiles
|
|
}
|
|
|
|
func runGeneration(oldGen map[[2]int]bool) map[[2]int]bool {
|
|
counts := make(map[[2]int]int)
|
|
newGen := make(map[[2]int]bool)
|
|
|
|
for coords, live := range oldGen {
|
|
if live {
|
|
counts[[2]int{coords[0] + 1, coords[1]}]++
|
|
counts[[2]int{coords[0] - 1, coords[1]}]++
|
|
counts[[2]int{coords[0], coords[1] + 1}]++
|
|
counts[[2]int{coords[0], coords[1] - 1}]++
|
|
counts[[2]int{coords[0] + 1, coords[1] - 1}]++
|
|
counts[[2]int{coords[0] - 1, coords[1] + 1}]++
|
|
}
|
|
}
|
|
|
|
for coords, count := range counts {
|
|
if oldGen[coords] && count <= 2 {
|
|
newGen[coords] = true
|
|
}
|
|
if !oldGen[coords] && count == 2 {
|
|
newGen[coords] = true
|
|
}
|
|
}
|
|
|
|
return newGen
|
|
}
|
|
|
|
func main() {
|
|
step := os.Args[1]
|
|
values := util.InputParserStrings(os.Args[2])
|
|
|
|
tiles := parseInput(values)
|
|
|
|
if step == "2" {
|
|
for i := 0; i < 100; i++ {
|
|
tiles = runGeneration(tiles)
|
|
}
|
|
}
|
|
|
|
count := 0
|
|
for _, v := range tiles {
|
|
if v == true {
|
|
count++
|
|
}
|
|
}
|
|
fmt.Println(count)
|
|
}
|