AoC2020/day23/day23.go
Karl Spickermann e572ed1862 Day23 Part 1
2020-12-23 22:07:52 +01:00

108 lines
2.2 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)
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
}