package day01

import (
	"adventofcode2024/utils"
	"sort"
	"strings"
)

func Part1(input string) int {
	var list1, list2 []int

	num := 0
	lines := strings.Split(input, "\n")
	for _, line := range lines {
		fields := strings.Fields(line)
		// Convert the fields to integers
		val1 := utils.MustAtoi(fields[0])
		val2 := utils.MustAtoi(fields[1])

		// Append to the respective slices
		list1 = append(list1, val1)
		list2 = append(list2, val2)
	}
	sort.Ints(list1)
	sort.Ints(list2)
	for i := 0; i < len(list1); i++ {
		num += utils.Abs(list1[i] - list2[i])
	}
	return num

}

func Part2(input string) int {
	var list1 []int
	list2 := make(map[int]int)


	num := 1
	lines := strings.Split(input, "\n")
	for _, line := range lines {
		fields := strings.Fields(line)
		// Convert the fields to integers
		val1 := utils.MustAtoi(fields[0])
		val2 := utils.MustAtoi(fields[1])

		// Append to the respective slices
		list1 = append(list1, val1)
		list2[val2]++
	}
	for i := 0; i < len(list1); i++ {
		num += list1[i] * list2[list1[i]]
	}
	return num
}
