-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
142 lines (130 loc) · 2.84 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
_ "embed"
"fmt"
"strconv"
"strings"
"unicode"
)
//go:embed input.txt
var input string
type coord struct {
x int
y int
}
func (c coord) borders(v coord) bool {
return c == coord{v.x, v.y + 1} ||
c == coord{v.x + 1, v.y + 1} ||
c == coord{v.x + 1, v.y} ||
c == coord{v.x + 1, v.y - 1} ||
c == coord{v.x, v.y - 1} ||
c == coord{v.x - 1, v.y - 1} ||
c == coord{v.x - 1, v.y} ||
c == coord{v.x - 1, v.y + 1}
}
type partNumber struct {
v int
len int
start coord
}
func main() {
g := parseGrid(input)
gearCoords := make([]coord, 0)
partNumbers := make([]partNumber, 0)
for y := range g {
for x := 0; x < len(g[y]); x++ {
// If anything but a number, carry on
// Otherwise we're at a number and need to build the full number
// by scanning ahead in the row. Iteration will continue from the next
// non-number position
v := g[y][x]
if v == '*' {
gearCoords = append(gearCoords, coord{x, y})
}
if !unicode.IsDigit(v) {
continue
}
num, endX, isPartNum := mustScanNumber(g, x, y)
fmt.Printf("Found number %d; isPartNum=%t\n", num, isPartNum)
if isPartNum {
partNumbers = append(partNumbers, partNumber{
v: num,
len: endX - x + 1,
start: coord{x, y},
})
}
x = endX
}
}
sum := 0
for _, c := range gearCoords {
fmt.Println("Checking gear:", c)
var partCount int
gearRatio := 1
for _, pn := range partNumbers {
for i := 0; i < pn.len; i++ {
if c.borders(coord{pn.start.x + i, pn.start.y}) {
fmt.Println("Borders pn:", pn)
gearRatio = gearRatio * pn.v
partCount++
break
}
}
}
if partCount == 2 {
fmt.Println("Adding ratio:", gearRatio)
sum += gearRatio
}
}
fmt.Println("Sum:", sum)
}
func parseGrid(input string) [][]rune {
lines := strings.Split(input, "\n")
grid := make([][]rune, 0, len(lines))
for _, l := range lines {
row := make([]rune, 0, len(l))
for _, r := range l {
row = append(row, r)
}
grid = append(grid, row)
}
return grid
}
func mustScanNumber(g [][]rune, x int, y int) (num int, endX int, isPartNum bool) {
digits := make([]rune, 0)
row := g[y]
for ; x < len(row); x++ {
if !unicode.IsDigit(row[x]) {
break
}
if checkForSymbol(g, x, y) {
isPartNum = true
}
digits = append(digits, row[x])
endX = x
}
num, err := strconv.Atoi(string(digits))
if err != nil {
panic(fmt.Errorf("parse digits: %w", err))
}
return num, endX, isPartNum
}
func checkForSymbol(g [][]rune, x int, y int) bool {
check := func(x int, y int) bool {
if x < 0 || y < 0 || y >= len(g) || x >= len(g[y]) {
return false
}
if g[y][x] == '.' || unicode.IsDigit(g[y][x]) {
return false
}
return true
}
return check(x, y+1) ||
check(x+1, y+1) ||
check(x+1, y) ||
check(x+1, y-1) ||
check(x, y-1) ||
check(x-1, y-1) ||
check(x-1, y) ||
check(x-1, y+1)
}