use a coord struct instead of Sprintf

This commit is contained in:
David 2023-12-03 20:33:05 -05:00
parent f4b07a0327
commit d529b002b4

View File

@ -33,7 +33,7 @@ func makeScanner(test bool) *bufio.Scanner {
return bufio.NewScanner(reader) return bufio.NewScanner(reader)
} }
type schematic map[string]entry type schematic map[coord]entry
type entry struct { type entry struct {
num []rune num []rune
@ -45,12 +45,13 @@ type tmp struct {
col int col int
} }
func key(row, col int) string { type coord struct {
return fmt.Sprintf("%d.%d", row, col) row int
col int
} }
func isSymbol(s schematic, row, col int) bool { func isSymbol(s schematic, row, col int) bool {
if c, ok := s[key(row, col)]; ok { if c, ok := s[coord{row, col}]; ok {
return c.symb != 0 return c.symb != 0
} }
return false return false
@ -76,17 +77,17 @@ func partOne() {
} }
default: default:
if len(i.tmp) != 0 { if len(i.tmp) != 0 {
schematic[key(row, i.col)] = entry{num: i.tmp} schematic[coord{row, i.col}] = entry{num: i.tmp}
i.tmp = []rune{} i.tmp = []rune{}
} }
if c != '.' { if c != '.' {
schematic[key(row, col)] = entry{symb: c} schematic[coord{row, col}] = entry{symb: c}
} }
} }
} }
// check if we have a number still to save but ran out of line // check if we have a number still to save but ran out of line
if len(i.tmp) != 0 { if len(i.tmp) != 0 {
schematic[key(row, i.col)] = entry{num: i.tmp} schematic[coord{row, i.col}] = entry{num: i.tmp}
} }
row++ row++
} }
@ -94,7 +95,7 @@ func partOne() {
// now, map back over the schematics and find the parts // now, map back over the schematics and find the parts
for row := 0; row <= 139; row++ { for row := 0; row <= 139; row++ {
for col := 0; col <= 139; col++ { for col := 0; col <= 139; col++ {
if entry, ok := schematic[key(row, col)]; ok && entry.symb == 0 { if entry, ok := schematic[coord{row, col}]; ok && entry.symb == 0 {
part := mustAtoi(entry.num) part := mustAtoi(entry.num)
if isSymbol(schematic, row, col-1) { if isSymbol(schematic, row, col-1) {
// look behind // look behind
@ -139,32 +140,32 @@ func partTwo() {
} }
default: default:
if len(i.tmp) != 0 { if len(i.tmp) != 0 {
schematic[key(row, i.col)] = entry{num: i.tmp} schematic[coord{row, i.col}] = entry{num: i.tmp}
i.tmp = []rune{} i.tmp = []rune{}
} }
if c == '*' { // no longer care about other symbols if c == '*' { // no longer care about other symbols
schematic[key(row, col)] = entry{symb: c} schematic[coord{row, col}] = entry{symb: c}
} }
} }
} }
// check if we have a number still to save but ran out of line // check if we have a number still to save but ran out of line
if len(i.tmp) != 0 { if len(i.tmp) != 0 {
schematic[key(row, i.col)] = entry{num: i.tmp} schematic[coord{row, i.col}] = entry{num: i.tmp}
} }
row++ row++
} }
gears := map[string]int{} gears := map[coord]int{}
sum := 0 sum := 0
// map back over the schematics and find the gears. // map back over the schematics and find the gears.
// there are only ever 1 or 2 parts attached to a gear. // there are only ever 1 or 2 parts attached to a gear.
for row := 0; row <= 139; row++ { for row := 0; row <= 139; row++ {
for col := 0; col <= 139; col++ { for col := 0; col <= 139; col++ {
if entry, ok := schematic[key(row, col)]; ok && entry.symb == 0 { if entry, ok := schematic[coord{row, col}]; ok && entry.symb == 0 {
offset := len(entry.num) offset := len(entry.num)
part := mustAtoi(entry.num) part := mustAtoi(entry.num)
if isSymbol(schematic, row, col-1) { if isSymbol(schematic, row, col-1) {
k := key(row, col-1) k := coord{row, col - 1}
if gears[k] != 0 { if gears[k] != 0 {
sum += gears[k] * part sum += gears[k] * part
} else { } else {
@ -172,7 +173,7 @@ func partTwo() {
} }
} }
if isSymbol(schematic, row, col+offset) { if isSymbol(schematic, row, col+offset) {
k := key(row, col+offset) k := coord{row, col + offset}
if gears[k] != 0 { if gears[k] != 0 {
sum += gears[k] * part sum += gears[k] * part
} else { } else {
@ -181,7 +182,7 @@ func partTwo() {
} }
for i := -1; i < offset+1; i++ { for i := -1; i < offset+1; i++ {
if isSymbol(schematic, row-1, col+i) { if isSymbol(schematic, row-1, col+i) {
k := key(row-1, col+i) k := coord{row - 1, col + i}
if gears[k] != 0 { if gears[k] != 0 {
sum += gears[k] * part sum += gears[k] * part
} else { } else {
@ -189,7 +190,7 @@ func partTwo() {
} }
} }
if isSymbol(schematic, row+1, col+i) { if isSymbol(schematic, row+1, col+i) {
k := key(row+1, col+i) k := coord{row + 1, col + i}
if gears[k] != 0 { if gears[k] != 0 {
sum += gears[k] * part sum += gears[k] * part
} else { } else {