package main

import (
	"fmt"
	"os"
	"strconv"
)

type Cup struct {
	Number int
	Next   *Cup
}

func main() {
	args := os.Args[1:]
	input := args[0]
	var cups  = make(map[int]*Cup)
	fmt.Println(input)

	for _, val := range input{
		number,_ := strconv.Atoi(string(val))
		cups[number] = &Cup{Number: number}
	}
	for id, val := range input {
		number,_ := strconv.Atoi(string(val))
		nextNumber, _ := strconv.Atoi(string(input[(id+1)%len(input)]))
		cups[number].Next = cups[nextNumber]
	}

	var inputNumbers []int
	for _, val := range input{
		number,_ := strconv.Atoi(string(val))
		inputNumbers = append(inputNumbers,number)
	}

	min, max := minMax(inputNumbers)
	iteration := 0
	currentCup := cups[inputNumbers[0]]
	for iteration < 100 {
		fmt.Println(currentCup.Number)
		threeCupStart := currentCup.Next
		threeCupEnd := threeCupStart.Next.Next
		currentCup.Next = threeCupEnd.Next
		destination := currentCup.Number-1
		for {
			if destination < min {
				destination = max
			}
			if destination != threeCupStart.Number &&
				destination != threeCupStart.Next.Number &&
				destination != threeCupEnd.Number {
				break
			}
			destination--
		}
		destinationCup := cups[destination]
		destinationNext := destinationCup.Next
		destinationCup.Next = threeCupStart
		threeCupEnd.Next = destinationNext
		currentCup = currentCup.Next
		iteration++
	}
	s := ""
	cup := cups[1].Next
	for cup != cups[1] {
		s += fmt.Sprint(cup.Number)
		cup = cup.Next
	}
	fmt.Println(s)
}

func runRound(cups *[]int){

}

func moveElement(cups *[]int, from int, to int) {
	indexToRemove := from
	indexWhereToInsert := to

	val := (*cups) [indexToRemove]

	(*cups) = append((*cups)[:indexToRemove], (*cups)[indexToRemove+1:]...)
	fmt.Println("cups:", (*cups))

	newSlice := make([]int, indexWhereToInsert+1)
	copy(newSlice,(*cups)[:indexWhereToInsert])
	newSlice[indexWhereToInsert]=val
	fmt.Println("newSlice:", newSlice)
	fmt.Println("cups:", (*cups))

	(*cups) = append(newSlice, (*cups)[indexWhereToInsert:]...)
	fmt.Println("cups:", (*cups))
}

func minMax(input[]int) (int, int){
	var max int
	var min int
	for i, val := range input {
		if i==0 || val < min {
			min = val
		}
		if i==0 || val > max {
			max = val
		}
	}
	return min,max
}
