aoc2024/day-04/internal/two/matrix.go

57 lines
888 B
Go
Raw Normal View History

2024-12-05 01:31:44 +00:00
package two
import (
"fmt"
"reflect"
log "github.com/sirupsen/logrus"
)
type matrix [3][3]rune
func newMatrix(mid, nw, ne, se, sw rune) *matrix {
return &matrix{
{nw, -1, ne},
{-1, mid, -1},
{sw, -1, se},
}
}
func (m *matrix) String() string {
temp := [3][3]string{}
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
temp[i][j] = string(m[i][j])
}
}
return fmt.Sprintf("\n%s\n%s\n%s", temp[0], temp[1], temp[2])
}
func (m *matrix) rotate() *matrix {
temp := matrix{}
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
temp[3-j-1][i] = m[i][j]
}
}
return &temp
}
func (m *matrix) isValid() bool {
2024-12-05 01:31:44 +00:00
golden := &matrix{
{'M', -1, 'M'},
{-1, 'A', -1},
{'S', -1, 'S'},
}
for range 4 {
if reflect.DeepEqual(m, golden) {
log.Debugf("%s \n---- %s", m.String(), golden.String())
return true
}
golden = golden.rotate()
}
return false
}