Moving to more powerful system

This commit is contained in:
Brian Buller 2017-12-16 08:43:42 -06:00
parent 106b099f68
commit 948d809c4f
4 changed files with 88 additions and 0 deletions

Binary file not shown.

86
2017/day16/day16.go Normal file
View File

@ -0,0 +1,86 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
)
func main() {
var progs string
length := 16
iters := 1000000000
if len(os.Args) > 1 {
length = Atoi(os.Args[1])
}
for i := 0; i < length; i++ {
progs += string('a' + i)
}
inp := StdinToString()
moves := strings.Split(inp, ",")
for i := 0; i < iters; i++ {
if i%(iters/100) == 0 {
// Clear the screen
fmt.Print("\033[H\033[2J")
// Percent Complete
fResult := ((float64(i) / float64(iters)) * 100)
sResult2 := strconv.FormatFloat(fResult, 'f', 2, 64)
fmt.Println(sResult2)
}
progs = doDance(progs, moves)
}
fmt.Println(progs)
}
func doDance(progs string, moves []string) string {
for _, v := range moves {
switch v[0] {
case 's':
progs = Spin(progs, Atoi(v[1:]))
case 'p':
pts := strings.Split(v[1:], "/")
progs = Partner(progs, pts[0], pts[1])
case 'x':
pts := strings.Split(v[1:], "/")
progs = Exchange(progs, Atoi(pts[0]), Atoi(pts[1]))
}
}
return progs
}
func Spin(progs string, cnt int) string {
stSpin := len(progs) - cnt
return progs[stSpin:] + progs[:stSpin]
}
func Exchange(progs string, i1, i2 int) string {
if i1 > i2 {
i1, i2 = i2, i1
}
return progs[:i1] + string(progs[i2]) + progs[i1+1:i2] + string(progs[i1]) + progs[i2+1:]
}
func Partner(progs string, p1, p2 string) string {
return Exchange(progs, strings.Index(progs, p1), strings.Index(progs, p2))
}
func StdinToString() string {
var input string
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
input = scanner.Text()
}
return input
}
func Atoi(i string) int {
var ret int
var err error
if ret, err = strconv.Atoi(i); err != nil {
log.Fatal("Invalid Atoi")
}
return ret
}

1
2017/day16/input Normal file

File diff suppressed because one or more lines are too long

1
2017/day16/testinput Normal file
View File

@ -0,0 +1 @@
s1,x3/4,pe/b