2017 Complete!
This commit is contained in:
BIN
2017/day21/2017day21.gif
Normal file
BIN
2017/day21/2017day21.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.4 MiB |
@@ -6,11 +6,13 @@ import (
|
||||
"math"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var rules map[string]string
|
||||
|
||||
func main() {
|
||||
numIters := 18
|
||||
rules = make(map[string]string)
|
||||
|
||||
inp := StdinToStrings()
|
||||
@@ -19,27 +21,46 @@ func main() {
|
||||
rules[pts[0]] = pts[2]
|
||||
rules[Flip(pts[0])] = pts[2]
|
||||
rules[Rotate(pts[0], 90)] = pts[2]
|
||||
rules[Flip(Rotate(pts[0], 90))] = pts[2]
|
||||
rules[Rotate(pts[0], 180)] = pts[2]
|
||||
rules[Flip(Rotate(pts[0], 180))] = pts[2]
|
||||
rules[Rotate(pts[0], 270)] = pts[2]
|
||||
rules[Flip(Rotate(pts[0], 270))] = pts[2]
|
||||
}
|
||||
|
||||
pattern := ".#./..#/###"
|
||||
for i := 0; i < 2; i++ {
|
||||
Tick(pattern)
|
||||
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)
|
||||
}
|
||||
/*
|
||||
pattern = rules[pattern]
|
||||
pts := SplitBlocks(pattern)
|
||||
for i := range pts {
|
||||
pts[i] = rules[pts[i]]
|
||||
}
|
||||
fmt.Println(pts)
|
||||
fmt.Println(CombineBlocks(pts))
|
||||
*/
|
||||
}
|
||||
|
||||
func Tick(pt string) string {
|
||||
fmt.Println(SplitBlocks(pt))
|
||||
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)
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -55,23 +76,43 @@ func SplitBlocks(pt string) []string {
|
||||
// 2x2 blocks
|
||||
blkSize = 2
|
||||
}
|
||||
|
||||
for j := 0; j < len(pts); j += blkSize {
|
||||
for i := 0; i < len(pts[j]); i += blkSize {
|
||||
ret = append(ret, pts[j][i:i+blkSize]+"/"+pts[j+1][i:i+blkSize])
|
||||
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])
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func CombineBlocks(pts []string) string {
|
||||
var ret string
|
||||
sz := int(math.Sqrt(float64(len(pts))))
|
||||
fmt.Println("Combining")
|
||||
fmt.Println(sz)
|
||||
//var j int
|
||||
for i := 0; i < len(pts); i++ {
|
||||
var ret []string
|
||||
if len(pts) == 1 {
|
||||
return pts[0]
|
||||
}
|
||||
return ret
|
||||
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, "/")
|
||||
}
|
||||
|
||||
func Flip(pt string) string {
|
||||
@@ -115,6 +156,10 @@ func RevString(s string) string {
|
||||
return string(runes)
|
||||
}
|
||||
|
||||
func ClearScreen() {
|
||||
fmt.Println("\033[H\033[2J")
|
||||
}
|
||||
|
||||
func StdinToStrings() []string {
|
||||
var input []string
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
|
Reference in New Issue
Block a user