package day11

import (
	"adventofcode2024/utils"
	"fmt"
	"strconv"
	"strings"
)

func Part1(input string) int {
	stones := strings.Fields(input)
	for i := 0; i < 25; i++ {
		n_stones := []string{}
		for _, stone := range stones {
			switch {
			case stone == "0":
				n_stones = append(n_stones, "1")
			case len(stone)%2 == 0:
				d1 := strconv.Itoa(utils.MustAtoi(stone[0 : len(stone)/2]))
				d2 := strconv.Itoa(utils.MustAtoi(stone[len(stone)/2:]))
				n_stones = append(n_stones, d1)
				n_stones = append(n_stones, d2)

			default:
				n_stones = append(n_stones, fmt.Sprintf("%d", utils.MustAtoi(stone)*2024))
			}
		}
		stones = n_stones
	}
	return len(stones)
}

func Part2(input string) int {
	stones := strings.Fields(input)
	smap := make(map[string]int)
	smap2 := make(map[string]int)

	for _, stone := range stones {
		smap[stone] = 1
	}
	for i:=0;i<75;i++ {
		for k, v := range smap {
			switch {
			case k == "0":
				smap2["1"] = smap2["1"] + v
			case len(k)%2 == 0:
				d1 := strconv.Itoa(utils.MustAtoi(k[0 : len(k)/2]))
				d2 := strconv.Itoa(utils.MustAtoi(k[len(k)/2:]))
				smap2[d1] += v
				smap2[d2] += v
			default:
				k2 := fmt.Sprintf("%d", utils.MustAtoi(k)*2024)
				smap2[k2] += v
			}
		}
		smap = CopyMap(smap2)
		smap2 = make(map[string]int)
	}
	count := 0
	for _, v := range smap {
		count += v
	}
	return count
}

func CopyMap(original map[string]int) map[string]int {
    // Create a new map to hold the copy
    copied := make(map[string]int)
    
    // Copy each key-value pair from the original map to the new map
    for key, value := range original {
        copied[key] = value
    }
    
    return copied
}
