2017 Day 16 & Day 17 Complete

This commit is contained in:
2017-12-17 10:00:37 -06:00
parent 948d809c4f
commit 2a79811b20
6 changed files with 287 additions and 10 deletions

View File

@@ -15,24 +15,33 @@ func main() {
iters := 1000000000
if len(os.Args) > 1 {
length = Atoi(os.Args[1])
if len(os.Args) > 2 {
iters = Atoi(os.Args[2])
}
}
// Create a starting string
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)
var loopEnd int
var state []string
doneProgs := progs
state = append(state, progs)
for loopEnd = 1; loopEnd < (iters / 2); loopEnd++ {
doneProgs = doDance(doneProgs, moves)
state = append(state, doneProgs)
if doneProgs == progs {
fmt.Println("Loops at", loopEnd)
pos := (iters / loopEnd) * loopEnd
fmt.Println("Final State", state[iters-pos])
break
}
progs = doDance(progs, moves)
}
fmt.Println(progs)
}
func doDance(progs string, moves []string) string {

78
2017/day16/problem Normal file
View File

@@ -0,0 +1,78 @@
Advent of Code
--- Day 16: Permutation Promenade ---
You come upon a very unusual sight; a group of programs here appear to be
dancing.
There are sixteen programs in total, named a through p. They start by
standing in a line: a stands in position 0, b stands in position 1, and so on
until p, which stands in position 15.
The programs' dance consists of a sequence of dance moves:
* Spin, written sX, makes X programs move from the end to the front, but
maintain their order otherwise. (For example, s3 on abcde produces
cdeab).
* Exchange, written xA/B, makes the programs at positions A and B swap
places.
* Partner, written pA/B, makes the programs named A and B swap places.
For example, with only five programs standing in a line (abcde), they could
do the following dance:
* s1, a spin of size 1: eabcd.
* x3/4, swapping the last two programs: eabdc.
* pe/b, swapping programs e and b: baedc.
After finishing their dance, the programs end up in order baedc.
You watch the dance for a while and record their dance moves (your puzzle
input). In what order are the programs standing after their dance?
Your puzzle answer was ________________.
--- Part Two ---
Now that you're starting to get a feel for the dance moves, you turn your
attention to the dance as a whole.
Keeping the positions they ended up in from their previous dance, the
programs perform it again and again: including the first dance, a total of
one billion (1000000000) times.
In the example above, their second dance would begin with the order baedc,
and use the same dance moves:
* s1, a spin of size 1: cbaed.
* x3/4, swapping the last two programs: cbade.
* pe/b, swapping programs e and b: ceadb.
In what order are the programs standing after their billion dances?
Your puzzle answer was ________________.
Both parts of this puzzle are complete! They provide two gold stars: **
At this point, you should return to your advent calendar and try another
puzzle.
If you still want to see it, you can get your puzzle input.
References
Visible links
. http://adventofcode.com/
. http://adventofcode.com/2017/about
. http://adventofcode.com/2017/support
. http://adventofcode.com/2017/events
. http://adventofcode.com/2017/settings
. http://adventofcode.com/2017/auth/logout
. http://adventofcode.com/2017
. http://adventofcode.com/2017
. http://adventofcode.com/2017/leaderboard
. http://adventofcode.com/2017/stats
. http://adventofcode.com/2017/sponsors
. http://adventofcode.com/2017/sponsors
. http://adventofcode.com/2017
. http://adventofcode.com/2017/day/16/input