package day04

import (
	"strings"
	"strconv"
//	"fmt"

	mapset "github.com/deckarep/golang-set/v2"

)

func Part1(input string) int {
	var count int = 0
	lines := strings.Split(input, "\n")
	for _, line := range lines {
		pairs := strings.Split(line, ",")
		
		pair1 := strings.Split(pairs[0], "-")
		pair2 := strings.Split(pairs[1], "-")

		p1set := mapset.NewSet[int](Range(pair1[0], pair1[1])...)
		p2set := mapset.NewSet[int](Range(pair2[0], pair2[1])...)

		if p1set.IsSubset(p2set) || p2set.IsSubset(p1set) {
			count++
		}
	}
	return count
}

func Part2(input string) int {
	var count int = 0
	lines := strings.Split(input, "\n")
	for _, line := range lines {
		pairs := strings.Split(line, ",")
		
		pair1 := strings.Split(pairs[0], "-")
		pair2 := strings.Split(pairs[1], "-")

		p1set := mapset.NewSet[int](Range(pair1[0], pair1[1])...)
		p2set := mapset.NewSet[int](Range(pair2[0], pair2[1])...)

		if len(p1set.Intersect(p2set).ToSlice()) > 0 {
			count++
		}
	}
	return count
}

func Range(start string, end string)[]int {
	s, _ := strconv.Atoi(start)
	e, _ := strconv.Atoi(end)

	var r []int
	for i := s; i <= e; i++ {
		r = append(r, i)
	}
	return r
}