use a coord struct instead of Sprintf
This commit is contained in:
parent
f4b07a0327
commit
d529b002b4
35
03/main.go
35
03/main.go
@ -33,7 +33,7 @@ func makeScanner(test bool) *bufio.Scanner {
|
||||
return bufio.NewScanner(reader)
|
||||
}
|
||||
|
||||
type schematic map[string]entry
|
||||
type schematic map[coord]entry
|
||||
|
||||
type entry struct {
|
||||
num []rune
|
||||
@ -45,12 +45,13 @@ type tmp struct {
|
||||
col int
|
||||
}
|
||||
|
||||
func key(row, col int) string {
|
||||
return fmt.Sprintf("%d.%d", row, col)
|
||||
type coord struct {
|
||||
row int
|
||||
col int
|
||||
}
|
||||
|
||||
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 false
|
||||
@ -76,17 +77,17 @@ func partOne() {
|
||||
}
|
||||
default:
|
||||
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{}
|
||||
}
|
||||
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
|
||||
if len(i.tmp) != 0 {
|
||||
schematic[key(row, i.col)] = entry{num: i.tmp}
|
||||
schematic[coord{row, i.col}] = entry{num: i.tmp}
|
||||
}
|
||||
row++
|
||||
}
|
||||
@ -94,7 +95,7 @@ func partOne() {
|
||||
// now, map back over the schematics and find the parts
|
||||
for row := 0; row <= 139; row++ {
|
||||
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)
|
||||
if isSymbol(schematic, row, col-1) {
|
||||
// look behind
|
||||
@ -139,32 +140,32 @@ func partTwo() {
|
||||
}
|
||||
default:
|
||||
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{}
|
||||
}
|
||||
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
|
||||
if len(i.tmp) != 0 {
|
||||
schematic[key(row, i.col)] = entry{num: i.tmp}
|
||||
schematic[coord{row, i.col}] = entry{num: i.tmp}
|
||||
}
|
||||
row++
|
||||
}
|
||||
|
||||
gears := map[string]int{}
|
||||
gears := map[coord]int{}
|
||||
sum := 0
|
||||
// map back over the schematics and find the gears.
|
||||
// there are only ever 1 or 2 parts attached to a gear.
|
||||
for row := 0; row <= 139; row++ {
|
||||
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)
|
||||
part := mustAtoi(entry.num)
|
||||
if isSymbol(schematic, row, col-1) {
|
||||
k := key(row, col-1)
|
||||
k := coord{row, col - 1}
|
||||
if gears[k] != 0 {
|
||||
sum += gears[k] * part
|
||||
} else {
|
||||
@ -172,7 +173,7 @@ func partTwo() {
|
||||
}
|
||||
}
|
||||
if isSymbol(schematic, row, col+offset) {
|
||||
k := key(row, col+offset)
|
||||
k := coord{row, col + offset}
|
||||
if gears[k] != 0 {
|
||||
sum += gears[k] * part
|
||||
} else {
|
||||
@ -181,7 +182,7 @@ func partTwo() {
|
||||
}
|
||||
for i := -1; i < offset+1; i++ {
|
||||
if isSymbol(schematic, row-1, col+i) {
|
||||
k := key(row-1, col+i)
|
||||
k := coord{row - 1, col + i}
|
||||
if gears[k] != 0 {
|
||||
sum += gears[k] * part
|
||||
} else {
|
||||
@ -189,7 +190,7 @@ func partTwo() {
|
||||
}
|
||||
}
|
||||
if isSymbol(schematic, row+1, col+i) {
|
||||
k := key(row+1, col+i)
|
||||
k := coord{row + 1, col + i}
|
||||
if gears[k] != 0 {
|
||||
sum += gears[k] * part
|
||||
} else {
|
||||
|
Loading…
Reference in New Issue
Block a user