advent-of-code

Perserverance, or the lack thereof

git clone git://git.shimmy1996.com/advent-of-code.git

main.go (1545B)

    1 package main
    2 
    3 import (
    4 	"fmt"
    5 )
    6 
    7 func main() {
    8 	input := []int{3, 6, 8, 1, 9, 5, 7, 4, 2}
    9 	// 95648732
   10 	fmt.Println(part1(input))
   11 	// 192515314252
   12 	fmt.Println(part2(input))
   13 }
   14 
   15 func playMixCup(curr int, cups []int, steps int) []int {
   16 	min, max := 1, len(cups)-1 // Assumption based on input.
   17 	for i := 0; i < steps; i++ {
   18 		nextThree := cups[curr]
   19 		cups[curr] = cups[cups[cups[nextThree]]]
   20 		destVal := curr
   21 		for inNextThree := true; inNextThree; {
   22 			destVal -= 1
   23 			if destVal < min {
   24 				destVal = max
   25 			}
   26 			inNextThree = destVal == nextThree ||
   27 				destVal == cups[nextThree] ||
   28 				destVal == cups[cups[nextThree]]
   29 		}
   30 		destNext := cups[destVal]
   31 		cups[destVal] = nextThree
   32 		cups[cups[cups[nextThree]]] = destNext
   33 		curr = cups[curr]
   34 	}
   35 	return cups
   36 }
   37 
   38 func part1(input []int) (res string) {
   39 	// Represent the ring with slice, where cups[i] = number after i.
   40 	cups := make([]int, len(input)+1) // +1 to make indexing easier.
   41 	cups[input[len(input)-1]] = input[0]
   42 	for i, v := range input[:len(input)-1] {
   43 		cups[v] = input[i+1]
   44 	}
   45 	cups = playMixCup(input[0], cups, 100)
   46 	for i := cups[1]; i != 1; i = cups[i] {
   47 		res += string('0' + i)
   48 	}
   49 	return
   50 }
   51 
   52 func part2(input []int) (res int64) {
   53 	maxLen := 1000000
   54 	cups := make([]int, maxLen+1)
   55 	cups[maxLen] = input[0]
   56 	for i, v := range input[:len(input)-1] {
   57 		cups[v] = input[i+1]
   58 	}
   59 	cups[input[len(input)-1]] = len(input) + 1
   60 	for i := len(input) + 1; i < maxLen; i++ {
   61 		cups[i] = i + 1
   62 	}
   63 	cups = playMixCup(input[0], cups, 10000000)
   64 	return int64(cups[1]) * int64(cups[cups[1]])
   65 }