Day 15 Complete

This commit is contained in:
Brian Buller 2016-12-15 08:36:57 -06:00
parent 03e1bbbc43
commit 1e8357b160
5 changed files with 197 additions and 4 deletions

BIN
day15/day15 Executable file

Binary file not shown.

6
day15/input Normal file
View File

@ -0,0 +1,6 @@
Disc #1 has 17 positions; at time=0, it is at position 15.
Disc #2 has 3 positions; at time=0, it is at position 2.
Disc #3 has 19 positions; at time=0, it is at position 4.
Disc #4 has 13 positions; at time=0, it is at position 2.
Disc #5 has 7 positions; at time=0, it is at position 2.
Disc #6 has 5 positions; at time=0, it is at position 0.

118
day15/main.go Normal file
View File

@ -0,0 +1,118 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
)
func main() {
input := stdinToStringSlice()
var verbose, pt2 bool
var allDiscs []Disc
if len(os.Args) > 1 {
for i := range os.Args {
switch os.Args[i] {
case "-2": // Do part 2
pt2 = true
case "-v": // Verbose mode
verbose = true
}
}
}
for i := range input {
pts := strings.Fields(input[i])
id := atoi(pts[1][1:])
ttl := atoi(pts[3])
curr := atoi(pts[11][0 : len(pts[11])-1])
allDiscs = append(allDiscs, *CreateDisc(id, ttl, curr))
}
if pt2 {
d := CreateDisc(7, 11, 0)
allDiscs = append(allDiscs, *d)
}
var numTicks int
for !AllDiscsAlign(allDiscs) {
numTicks++
TickAllDiscs(allDiscs)
if verbose {
PrintStatus(allDiscs)
}
}
fmt.Println("Total Ticks: ", numTicks)
}
func PrintStatus(d []Disc) {
fmt.Print("[ ")
for i := range d {
fmt.Printf("(%2d/%2d) ", d[i].currPos, d[i].ttlPos)
}
fmt.Println("]")
}
func TickAllDiscs(d []Disc) {
for i := range d {
d[i].Tick()
}
}
func AllDiscsAlign(d []Disc) bool {
for i := range d {
if (d[i].currPos+d[i].id)%d[i].ttlPos != 0 {
return false
}
}
return true
}
type Disc struct {
id int
ttlPos int
currPos int
startPos int
numTicks int
bufTicks int
}
func CreateDisc(id, ttl, curr int) *Disc {
d := new(Disc)
d.id = id
d.ttlPos = ttl
d.currPos = curr
d.startPos = curr
return d
}
func (d *Disc) Tick() {
d.currPos = (d.currPos + 1) % d.ttlPos
d.numTicks++
}
func stdinToStringSlice() []string {
var input []string
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
input = append(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
}
func ClearScreen() {
fmt.Print("\033[H\033[2J")
}

65
day15/problem Normal file
View File

@ -0,0 +1,65 @@
Advent of Code
--- Day 15: Timing is Everything ---
The halls open into an interior plaza containing a large kinetic sculpture. The sculpture is in a sealed enclosure and seems to involve a set of identical spherical capsules that are carried to the top and allowed to bounce through
the maze of spinning pieces.
Part of the sculpture is even interactive! When a button is pressed, a capsule is dropped and tries to fall through slots in a set of rotating discs to finally go through a little hole at the bottom and come out of the sculpture. If
any of the slots aren't aligned with the capsule as it passes, the capsule bounces off the disc and soars away. You feel compelled to get one of those capsules.
The discs pause their motion each second and come in different sizes; they seem to each have a fixed number of positions at which they stop. You decide to call the position with the slot 0, and count up for each position it reaches
next.
Furthermore, the discs are spaced out so that after you push the button, one second elapses before the first disc is reached, and one second elapses as the capsule passes from one disk to the one below it. So, if you push the button
at time=100, then the capsule reaches the top disc at time=101, the second disc at time=102, the third disc at time=103, and so on.
The button will only drop a capsule at an integer time - no fractional seconds allowed.
For example, at time=0, suppose you see the following arrangement:
Disc #1 has 5 positions; at time=0, it is at position 4.
Disc #2 has 2 positions; at time=0, it is at position 1.
If you press the button exactly at time=0, the capsule would start to fall; it would reach the first disc at time=1. Since the first disc was at position 4 at time=0, by time=1 it has ticked one position forward. As a five-position
disc, the next position is 0, and the capsule falls through the slot.
Then, at time=2, the capsule reaches the second disc. The second disc has ticked forward two positions at this point: it started at position 1, then continued to position 0, and finally ended up at position 1 again. Because there's
only a slot at position 0, the capsule bounces away.
If, however, you wait until time=5 to push the button, then when the capsule reaches each disc, the first disc will have ticked forward 5+1 = 6 times (to position 0), and the second disc will have ticked forward 5+2 = 7 times (also
to position 0). In this case, the capsule would fall through the discs and come out of the machine.
However, your situation has more than two discs; you've noted their positions in your puzzle input. What is the first time you can press the button to get a capsule?
Your puzzle answer was _________.
--- Part Two ---
After getting the first capsule (it contained a star! what great fortune!), the machine detects your success and begins to rearrange itself.
When it's done, the discs are back in their original configuration as if it were time=0 again, but a new disc with 11 positions and starting at position 0 has appeared exactly one second below the previously-bottom disc.
With this new disc, and counting again starting from time=0 with the configuration in your puzzle input, what is the first time you can press the button to get another capsule?
Although it hasn't changed, you can still get your puzzle input.
Answer: _____________________
References
Visible links
. http://adventofcode.com/
. http://adventofcode.com/2016/about
. http://adventofcode.com/2016/support
. http://adventofcode.com/2016/events
. http://adventofcode.com/2016/settings
. http://adventofcode.com/2016/auth/logout
. http://adventofcode.com/2016
. http://adventofcode.com/2016
. http://adventofcode.com/2016/leaderboard
. http://adventofcode.com/2016/stats
. http://adventofcode.com/2016/sponsors
. http://adventofcode.com/2016/sponsors
. https://youtu.be/IxDoO9oODOk?t=177
. http://adventofcode.com/2016/day/15/input

View File

@ -17,10 +17,6 @@ func stdinToStringSlice() []string {
return input
}
func itoa(i int) string {
return strconv.Itoa(i)
}
func atoi(i string) int {
var ret int
var err error
@ -30,6 +26,10 @@ func atoi(i string) int {
return ret
}
func itoa(i int) string {
return strconv.Itoa(i)
}
func stdinToString() string {
var input string
scanner := bufio.NewScanner(os.Stdin)
@ -48,3 +48,7 @@ func fileToString(fn string) string {
}
return string(c)
}
func ClearScreen() {
fmt.Print("\033[H\033[2J")
}