From ed95f875ad46c219b39e583bae279e25ca58aa9a Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Fri, 25 Dec 2020 21:18:34 +0000 Subject: [PATCH] Day 24 solved. --- 2020/day24.go | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 2020/day24.go diff --git a/2020/day24.go b/2020/day24.go new file mode 100644 index 0000000..d996917 --- /dev/null +++ b/2020/day24.go @@ -0,0 +1,98 @@ +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) +}