}
var numbers = map[string]int{
"abcefg": 0,
"cf": 1,
"acdeg": 2,
"acdfg": 3,
"bcdf": 4,
"abdfg": 5,
"abdefg": 6,
"acf": 7,
"abcdefg": 8,
"abcdfg": 9,
}
const chars = "abcdefg"
type decodeKey map[rune]rune
func solveSecond(notes Notes) int {
var sum int
for _, n := range notes {
var key = decodeKey{}
key.findKey(n, chars)
sum += key.getValue(n)
}
return sum
}
func (key decodeKey) getValue(note Note) int {
var ret [4]int
for i, out := range note.output {
decoded := strings.Map(func(in rune) rune {
return key[in]
}, out)
decoded = sortString(decoded)
ret[i] = numbers[decoded]
}
return ret[0]*1000 + ret[1]*100 + ret[2]*10 + ret[3]
}
func (key decodeKey) findKey(note Note, in string) bool {
if len(in) == 0 {
if key.checkKey(note) {
return true
}
return false
}
for i := range in {
key[rune(chars[len(key)])] = rune(in[i])
if key.findKey(note, in[:i]+in[i+1:]) {
return true
} else {
delete(key, rune(chars[len(key)-1]))
}
}
return false
}
func (key decodeKey) checkKey(note Note) bool {
for _, digit := range note.signalPatterns {
decoded := strings.Map(func(in rune) rune {
return key[in]
}, digit)
decoded = sortString(decoded)
if _, ok := numbers[decoded]; !ok {
return false
}
}
return true