a little more refactoring
This commit is contained in:
parent
4e64387873
commit
561b10916d
66
03/main.go
66
03/main.go
@ -45,8 +45,8 @@ type coord struct {
|
|||||||
col int
|
col int
|
||||||
}
|
}
|
||||||
|
|
||||||
func isSymbol(s schematic, row, col int) bool {
|
func isSymbol(s schematic, c coord) bool {
|
||||||
if c, ok := s[coord{row, col}]; ok {
|
if c, ok := s[c]; ok {
|
||||||
return c.symb != 0
|
return c.symb != 0
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
@ -87,20 +87,13 @@ func partOne() {
|
|||||||
for col := 0; col <= 139; col++ {
|
for col := 0; col <= 139; col++ {
|
||||||
if entry, ok := schematic[coord{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, coord{row, col - 1}) || isSymbol(schematic, coord{row, col + len(entry.num)}) {
|
||||||
// look behind
|
// look ahead and look behind
|
||||||
sum += part
|
|
||||||
}
|
|
||||||
if isSymbol(schematic, row, col+len(entry.num)) {
|
|
||||||
// look ahead
|
|
||||||
sum += part
|
sum += part
|
||||||
}
|
}
|
||||||
for i := -1; i < len(entry.num)+1; i++ {
|
for i := -1; i < len(entry.num)+1; i++ {
|
||||||
// check row above and below
|
// check row above and below
|
||||||
if isSymbol(schematic, row-1, col+i) {
|
if isSymbol(schematic, coord{row - 1, col + i}) || isSymbol(schematic, coord{row + 1, col + i}) {
|
||||||
sum += part
|
|
||||||
}
|
|
||||||
if isSymbol(schematic, row+1, col+i) {
|
|
||||||
sum += part
|
sum += part
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -149,38 +142,31 @@ func partTwo() {
|
|||||||
if entry, ok := schematic[coord{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) {
|
found := false
|
||||||
k := coord{row, col - 1}
|
k := coord{}
|
||||||
if gears[k] != 0 {
|
if isSymbol(schematic, coord{row, col - 1}) {
|
||||||
sum += gears[k] * part
|
k = coord{row, col - 1}
|
||||||
} else {
|
found = true
|
||||||
gears[k] = part
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if isSymbol(schematic, row, col+offset) {
|
if isSymbol(schematic, coord{row, col + offset}) {
|
||||||
k := coord{row, col + offset}
|
k = coord{row, col + offset}
|
||||||
if gears[k] != 0 {
|
found = true
|
||||||
sum += gears[k] * part
|
|
||||||
} else {
|
|
||||||
gears[k] = part
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
for i := -1; i < offset+1; i++ {
|
for i := -1; i < offset+1; i++ {
|
||||||
if isSymbol(schematic, row-1, col+i) {
|
if isSymbol(schematic, coord{row - 1, col + i}) {
|
||||||
k := coord{row - 1, col + i}
|
k = coord{row - 1, col + i}
|
||||||
if gears[k] != 0 {
|
found = true
|
||||||
sum += gears[k] * part
|
|
||||||
} else {
|
|
||||||
gears[k] = part
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if isSymbol(schematic, row+1, col+i) {
|
if isSymbol(schematic, coord{row + 1, col + i}) {
|
||||||
k := coord{row + 1, col + i}
|
k = coord{row + 1, col + i}
|
||||||
if gears[k] != 0 {
|
found = true
|
||||||
sum += gears[k] * part
|
}
|
||||||
} else {
|
}
|
||||||
gears[k] = part
|
if found {
|
||||||
}
|
if gears[k] == 0 {
|
||||||
|
gears[k] = part
|
||||||
|
} else {
|
||||||
|
sum += gears[k] * part
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user