package day25

import (
	"fmt"
	"strings"
	"math"
)

type Lock struct {
	diagram string
	combination int
}

type Key struct {
	diagram string
	combination int
}

type Pair struct {
	l Lock
	k Key
}

func Part1(input string) int {
	data := strings.Split(input, "\n\n")
	locks := make([]Lock, 0)
	keys := make([]Key, 0)
	pairs := make([]Pair, 0)

	for _, in := range data {
		if in[0] == '#' {
			locks = append(locks, Lock{diagram: in, combination: combination(in, '.')  })
		} else {
			keys = append(keys, Key{diagram: in, combination: combination(in, '#')})
		}
	}
	
	for _, l := range locks {
		for _, k := range keys {
			if key_fits(l.combination, k.combination) {
				pairs = append(pairs, Pair{l: l, k: k})
			}
		}
	}
	fmt.Printf("locks: %d, keys: %d\n", len(locks), len(keys))
	fmt.Printf("pair0 lock\n%s\n%d\n", pairs[0].l.diagram , pairs[0].l.combination)
	fmt.Printf("pair0 key\n%s\n%d\n",pairs[0].k.diagram, pairs[0].k.combination)
	return len(pairs)
}

func Part2(input string) int {
	return 0
}

func combination(d string, k rune) int {
	comb := 0
	rows := strings.Split(d, "\n")
	columns := len(rows[0])
	for _, r := range rows {
		for i, c := range r {
			if c == k {
				comb += 1 * int(math.Pow10(columns - i - 1))
			}
		}
	}
	return comb
}

func key_fits(l int, k int) bool {
	for i:=1;i<6;i++ {
		ll := l % int(math.Pow10(i))
		kk := k % int(math.Pow10(i))
		if ll  < kk { return false }
	}
	return true
}