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