chore: cleanup day 3

This commit is contained in:
xuu 2023-12-03 12:59:14 -07:00
parent c894853cbd
commit 0f90769015
Signed by: xuu
GPG Key ID: 8B3B0604F164E04F

View File

@ -27,7 +27,7 @@ type symbol struct {
}
type symbolTab map[int]map[int]*symbol
func hasSymbol(tab symbolTab, row, col int, p partNumber) bool {
func (tab symbolTab) hasSymbol(row, col int, p partNumber) bool {
if cols, ok := tab[row]; ok {
s, ok := cols[col]
if ok {
@ -38,14 +38,13 @@ func hasSymbol(tab symbolTab, row, col int, p partNumber) bool {
}
return false
}
func scanSymbol(tab symbolTab, p partNumber) bool {
func (tab symbolTab) scanSymbol(p partNumber) bool {
rowStart, rowEnd := max(p.row-1, 0), p.row+1
colStart, colEnd := max(p.col-1, 0), p.end+1
for i := rowStart; i <= rowEnd; i++ {
for j := colStart; j <= colEnd; j++ {
ok := hasSymbol(tab, i, j, p)
// fmt.Println(p.number, i, j, ok)
ok := tab.hasSymbol(i, j, p)
if ok {
return true
}
@ -54,19 +53,20 @@ func scanSymbol(tab symbolTab, p partNumber) bool {
return false
}
// 553079
// 84363105
func main() {
buf := bytes.NewReader(input)
scan := bufio.NewScanner(buf)
m := [][]rune{}
parts := []partNumber{}
symbols := make(symbolTab)
symbolList := []*symbol{}
row := 0
for scan.Scan() {
text := scan.Text()
row := len(m)
m = append(m, []rune(text))
row += 1
slice := make([]rune, 0, 3)
var col int
@ -100,7 +100,7 @@ func main() {
sum := 0
for i, p := range parts {
ok := scanSymbol(symbols, p)
ok := symbols.scanSymbol(p)
parts[i].hasSymbol = ok
if ok {
sum += p.number
@ -114,10 +114,9 @@ func main() {
}
}
fmt.Println(m)
fmt.Println(parts)
fmt.Println(symbols)
fmt.Println(symbolList)
fmt.Println(sum)
fmt.Println(sumGears)
// fmt.Println(parts)
// fmt.Println(symbols)
// fmt.Println(symbolList)
fmt.Println("part1:", sum)
fmt.Println("part2:", sumGears)
}