-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgears.go
187 lines (152 loc) · 4.71 KB
/
gears.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// ___ __ ____
// | \ __ _ _ _ / \__ /
// | |) / _` | || | | () |_ \
// |___/\__,_|\_, | \__/___/
// |__/
//
// "Gear Ratios"
//
// Given a known number of cubes of three different colors [red, green, blue]
// determine if it's possible for given sequence of combinations to be presented.
//
package main
import (
"bufio"
"fmt"
"os"
"regexp"
"strconv"
"strings"
)
const FILENAME = "input.txt"
const DEBUG = false
var reNumber = regexp.MustCompile(`\d+`)
var reSymbols = regexp.MustCompile(`[^0-9\.]`)
var reAsterisk = regexp.MustCompile(`\*`)
// ^^ This is dumb, but writing a strings.Index() wrapper that supports
// multiple results would be annoying and not a lot faster at this scale.
// Simple wrapper for debug printing
func debugPrint(template string, data ...interface{}) {
if DEBUG {
fmt.Printf(template, data...)
}
}
func main() {
file, err := os.Open(FILENAME)
if err != nil {
panic(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
// We can't process line-at-a-time, read the whole thing:
var schematic []string
for scanner.Scan() {
schematic = append(schematic, scanner.Text())
}
// Find the row and start/end cols for each "part number" in the schematic:
var allParts [][]int
for row, data := range schematic {
partLocations := reNumber.FindAllStringSubmatchIndex(data, -1)
debugPrint("Input row %d:", row)
for _, cols := range partLocations {
partNumber, err := strconv.Atoi(data[cols[0]:cols[1]])
if err != nil {
panic(err)
}
allParts = append(allParts, []int{partNumber, row, cols[0], cols[1]})
debugPrint(" %d at %d:%d,", partNumber, cols[0], cols[1])
}
debugPrint("\n")
}
var realPartNumbers []int
sumRealPartNumbers := 0
// Now for each part, find the adjacent characters:
for _, partLoc := range allParts {
// Make these easier to read
partNumber := partLoc[0]
top := partLoc[1] - 1
bottom := partLoc[1] + 1
left := partLoc[2] - 1
right := partLoc[3] + 1
// And if any start/end marker is out of bounds, crop the search area
if top < 0 {
top = 0
}
if left < 0 {
left = 0
}
if right > len(schematic[0]) {
right = len(schematic[0])
}
if bottom > len(schematic) {
bottom = len(schematic)
}
// Something about inclusive-exclusive slicing makes doing this with a range
// problematic. I'd like to figure out a better way to do this.
var field []string
debugPrint("\n\nArea %v is [rows %d-%d cols %d-%d]:\n", partNumber, top, bottom, left, right)
// Get the first two rows of the search area
for _, text := range schematic[top:bottom] {
field = append(field, text[left:right])
}
// Get the last row of the search area if `bottom` isn't out of range
if bottom < len(schematic) {
field = append(field, schematic[bottom][left:right])
}
debugPrint(" %v\n", strings.Join(field, "\n "))
// Does this field have a symbol in it that isn't a period or a digit?
if reSymbols.MatchString(strings.Join(field, "")) {
debugPrint(" REAL\n")
realPartNumbers = append(realPartNumbers, partNumber)
sumRealPartNumbers += partNumber
}
}
debugPrint("\n")
// Part One:
// Count of part numbers: 1084
// Sum of these numbers: 539713
fmt.Printf("Count of part numbers: %v\n", len(realPartNumbers))
fmt.Printf("Sum of these numbers: %d\n", sumRealPartNumbers)
//
// ___ _ ___
// | _ \__ _ _ _| |_ |_ )
// | _/ _` | '_| _| / /
// |_| \__,_|_| \__| /___|
//
// Now we need to find the "gears" -- an asterisk which is adjacent to exactly
// two part numbers. The "gear ratio" is the product of those two adjacent
// numbers; report the sum of all the ratios.
var allGears [][]int
sumGearRatios := 0
// Find the row and col for each asterisk in the schematic:
for row, data := range schematic {
gearLocations := reAsterisk.FindAllStringSubmatchIndex(data, -1)
for _, cols := range gearLocations {
allGears = append(allGears, []int{row, cols[0]})
}
}
debugPrint("\nFound gears at coords:\n%v\n", allGears)
// Cycle through each and see if there are exactly two neighboring part numbers.
for _, gear := range allGears {
debugPrint("\nSearching around %v\n", gear)
var neighbors []int
for _, part := range allParts {
top := part[1] - 1
bottom := part[1] + 1
left := part[2] - 1
right := part[3] + 1
if top <= gear[0] && gear[0] <= bottom && left <= gear[1] && gear[1] < right {
debugPrint("Gear at %v is a neighbor to %v\n", gear, part)
neighbors = append(neighbors, part[0])
}
}
if len(neighbors) == 2 {
ratio := neighbors[0] * neighbors[1]
debugPrint("Ratio: %d\n", ratio)
sumGearRatios += ratio
}
}
// Part Two:
// Sum of all gear ratios: 84159075
fmt.Printf("\nSum of all gear ratios: %d\n", sumGearRatios)
}