AoC2020/day23/day23.go
Karl Spickermann 39ce3a0dea Day23 Part 2
2020-12-23 22:32:12 +01:00

112 lines
2.3 KiB
Go

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)
var inputNumbers []int
for _, val := range input{
number,_ := strconv.Atoi(string(val))
inputNumbers = append(inputNumbers,number)
}
_, maxInput := minMax(inputNumbers)
length := len(inputNumbers)
for i := 0; i < 1000000-length; i++ {
inputNumbers = append(inputNumbers, maxInput+1+i)
}
for _, val := range inputNumbers{
cups[val] = &Cup{Number: val}
}
for id, val := range inputNumbers {
number := val
nextNumber := inputNumbers[(id+1)%len(inputNumbers)]
cups[number].Next = cups[nextNumber]
}
min, max := minMax(inputNumbers)
iteration := 0
currentCup := cups[inputNumbers[0]]
for iteration < 10000000 {
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++
}
fmt.Println("Fineshed")
firstCup := cups[1]
fmt.Println(firstCup.Next.Number)
fmt.Println(firstCup.Next.Next.Number)
//s := ""
//cup := cups[1].Next
//for cup != cups[1] {
// 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
}