-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
98 lines (84 loc) · 1.91 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
package main
import (
_ "embed"
"fmt"
"slices"
"strconv"
"strings"
)
/**
* Day 3: Gear Ratios - Part 1
* url: https://adventofcode.com/2023/day/3
*
* Part 1: Find all numbers in the grid adjacent to a symbol
* Part 2: I preferred to do it in a separated file, too different implementation
*/
//go:embed input.txt
var input string
var digits = [...]string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
var symbols = [...]string{"#", "$", "+", "*", "%", "@", "&", "-", "/", "="}
func main() {
// create a mask to find where are valid numbers
lines := strings.Split(strings.TrimSpace(input), "\n")
// create binary mask
mask := make([][]bool, len(lines))
for i, s := range lines {
s = strings.TrimSpace(s)
mask[i] = make([]bool, len(s))
}
// set the mask, every neighbor of a symbol is valid
for j, s := range lines {
s = strings.TrimSpace(s)
for i := 0; i < len(s); i++ {
if !slices.Contains(symbols[:], string(s[i])) {
continue
}
// for neighbors of a symbol, set the mask
for x := -1; x < 2; x++ {
for y := -1; y < 2; y++ {
x1 := i + x
y1 := j + y
if mask[y1][x1] {
continue
}
if x1 >= 0 && x1 < len(s) && y1 >= 0 && y1 < len(lines) {
mask[y1][x1] = true
}
}
}
}
}
// find all numbers
numbers := []int{}
for j, s := range lines {
s = strings.TrimSpace(s)
buffer := ""
bufferValid := false
for i := 0; i < len(s); i++ {
char := string(s[i])
if slices.Contains(symbols[:], char) || char == "." {
if bufferValid {
num, _ := strconv.Atoi(buffer)
numbers = append(numbers, num)
}
buffer = ""
bufferValid = false
continue
}
buffer += char
if mask[j][i] {
bufferValid = true
}
}
if bufferValid {
num, _ := strconv.Atoi(buffer)
numbers = append(numbers, num)
}
}
// sum all numbers
sum := 0
for _, num := range numbers {
sum = sum + num
}
fmt.Println("Sum:", sum)
}