adventofcode/2017/day21/day21.go

171 lines
3.5 KiB
Go
Raw Normal View History

2017-12-22 18:45:49 +00:00
package main
import (
"bufio"
"fmt"
"math"
"os"
"strings"
2017-12-26 20:12:20 +00:00
"time"
2017-12-22 18:45:49 +00:00
)
2017-12-23 15:01:32 +00:00
var rules map[string]string
2017-12-22 18:45:49 +00:00
func main() {
2017-12-26 20:12:20 +00:00
numIters := 18
2017-12-23 15:01:32 +00:00
rules = make(map[string]string)
2017-12-22 18:45:49 +00:00
inp := StdinToStrings()
for i := range inp {
2017-12-23 15:01:32 +00:00
pts := strings.Split(inp[i], " ")
rules[pts[0]] = pts[2]
rules[Flip(pts[0])] = pts[2]
rules[Rotate(pts[0], 90)] = pts[2]
2017-12-26 20:12:20 +00:00
rules[Flip(Rotate(pts[0], 90))] = pts[2]
2017-12-23 15:01:32 +00:00
rules[Rotate(pts[0], 180)] = pts[2]
2017-12-26 20:12:20 +00:00
rules[Flip(Rotate(pts[0], 180))] = pts[2]
2017-12-23 15:01:32 +00:00
rules[Rotate(pts[0], 270)] = pts[2]
2017-12-26 20:12:20 +00:00
rules[Flip(Rotate(pts[0], 270))] = pts[2]
2017-12-23 15:01:32 +00:00
}
pattern := ".#./..#/###"
2017-12-26 20:12:20 +00:00
PrettyPrint(pattern)
for i := 0; i < numIters; i++ {
ClearScreen()
PrettyPrint(pattern)
time.Sleep(time.Second / 2)
pattern = Tick(pattern)
}
fmt.Println("Bits On", strings.Count(pattern, "#"))
}
func PrettyPrint(inp string) {
for _, v := range strings.Split(inp, "/") {
fmt.Println(v)
}
2017-12-22 18:45:49 +00:00
}
2017-12-23 15:01:32 +00:00
func Tick(pt string) string {
2017-12-26 20:12:20 +00:00
subPatterns := SplitBlocks(pt)
var tickSubs []string
for i := range subPatterns {
tickSubs = append(tickSubs, ApplyRule(subPatterns[i]))
}
return CombineBlocks(tickSubs)
}
func ApplyRule(inp string) string {
if v, ok := rules[inp]; ok {
return v
}
// This is an error.
fmt.Println("No rule for", inp)
os.Exit(1)
2017-12-23 15:01:32 +00:00
return ""
2017-12-22 18:45:49 +00:00
}
2017-12-23 15:01:32 +00:00
func SplitBlocks(pt string) []string {
var ret []string
pts := strings.Split(pt, "/")
if len(pts[0]) <= 3 {
return []string{pt}
2017-12-22 18:45:49 +00:00
}
2017-12-23 15:01:32 +00:00
// Assume 3x3
blkSize := 3
if len(pts[0])%2 == 0 {
// 2x2 blocks
blkSize = 2
2017-12-22 18:45:49 +00:00
}
2017-12-26 20:12:20 +00:00
2017-12-23 15:01:32 +00:00
for j := 0; j < len(pts); j += blkSize {
for i := 0; i < len(pts[j]); i += blkSize {
2017-12-26 20:12:20 +00:00
if blkSize == 2 {
ret = append(ret, pts[j][i:i+blkSize]+"/"+pts[j+1][i:i+blkSize])
} else {
ret = append(ret, pts[j][i:i+blkSize]+"/"+pts[j+1][i:i+blkSize]+"/"+pts[j+2][i:i+blkSize])
}
2017-12-22 18:45:49 +00:00
}
}
2017-12-23 15:01:32 +00:00
return ret
2017-12-22 18:45:49 +00:00
}
2017-12-23 15:01:32 +00:00
func CombineBlocks(pts []string) string {
2017-12-26 20:12:20 +00:00
var ret []string
if len(pts) == 1 {
return pts[0]
2017-12-22 18:45:49 +00:00
}
2017-12-26 20:12:20 +00:00
var subPts [][]string
for i := range pts {
subPts = append(subPts, strings.Split(pts[i], "/"))
}
subPtSz := len(subPts[0])
ptSize := int(math.Sqrt(float64(len(subPts) * subPtSz * subPtSz)))
w, h := (ptSize / subPtSz), (ptSize / subPtSz)
for i := 0; i < h; i++ {
st := i * w
for j := 0; j < subPtSz; j++ {
var bld string
for k := 0; k < w; k++ {
bld += subPts[st+k][j]
}
ret = append(ret, bld)
}
}
return strings.Join(ret, "/")
2017-12-22 18:45:49 +00:00
}
2017-12-23 15:01:32 +00:00
func Flip(pt string) string {
pts := strings.Split(pt, "/")
var newPts []string
for i := range pts {
newPts = append(newPts, RevString(pts[i]))
2017-12-22 18:45:49 +00:00
}
2017-12-23 15:01:32 +00:00
return strings.Join(newPts, "/")
2017-12-22 18:45:49 +00:00
}
2017-12-23 15:01:32 +00:00
func Rotate(pt string, deg int) string {
if deg == 0 {
return pt
2017-12-22 18:45:49 +00:00
}
2017-12-23 15:01:32 +00:00
if deg%90 != 0 {
// We can't do this
fmt.Println("Invalid Rotation:", deg)
os.Exit(1)
2017-12-22 18:45:49 +00:00
}
2017-12-23 15:01:32 +00:00
pts := strings.Split(pt, "/")
var tst string
if len(pts[0])%2 == 0 {
// 2x2 pattern
tst = string(pts[1][0]) + string(pts[0][0]) + "/"
tst += string(pts[1][1]) + string(pts[0][1])
return Rotate(tst, deg-90)
2017-12-22 18:45:49 +00:00
}
2017-12-23 15:01:32 +00:00
// 3x3 pattern
tst = string(pts[2][0]) + string(pts[1][0]) + string(pts[0][0]) + "/"
tst += string(pts[2][1]) + string(pts[1][1]) + string(pts[0][1]) + "/"
tst += string(pts[2][2]) + string(pts[1][2]) + string(pts[0][2])
return Rotate(tst, deg-90)
2017-12-22 18:45:49 +00:00
}
func RevString(s string) string {
runes := []rune(s)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}
2017-12-26 20:12:20 +00:00
func ClearScreen() {
fmt.Println("\033[H\033[2J")
}
2017-12-22 18:45:49 +00:00
func StdinToStrings() []string {
var input []string
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
input = append(input, scanner.Text())
}
return input
}