package day13

import (
	_ "adventofcode2023/utils"
	"adventofcode2023/utils/inputs"
	_ "fmt"
	"strings"
)

func Part1(input string) int {
	return symmetry(input, 0)
}

func Part2(input string) int {
	return symmetry(input, 1)
}

func symmetry(input string, smudges int) int {
	ans := 0

	patterns := strings.Split(input, "\n\n")
	for _, pattern := range patterns {
		grid := inputs.ToGrid2D[rune](pattern, "\n", "", ' ', func(c string) rune { return rune(c[0])})
		G := grid.Matrix()
		R := len(G)
		C := len(G[0])
		// vertical symmetry
		for c:=0;c<C-1;c++ {
			badness := 0
			for dc:=0;dc<C;dc++ {
				left := c-dc
				right := c+1+dc
				if left < 0 || left > right || right > C -1 { break }
				for r:=0;r<R;r++ {
					if G[r][left] != G[r][right] {
						badness += 1
					}
				}
			}
			if badness == smudges { ans += c+1 }
		}
		// horizontal symmetry
		for r:=0;r<R-1;r++ {
			badness := 0
			for dr:=0;dr<R;dr++ {
				up := r-dr
				down := r+1+dr
				if up < 0 || up > down || down > R -1 { break }
				for c:=0;c<C;c++ {
					if G[up][c] != G[down][c] {
						badness += 1
					}
				}
			}
			if badness == smudges { ans += 100*(r+1) }
		}
	}
	return ans
}