-
Notifications
You must be signed in to change notification settings - Fork 0
/
03p1.go
96 lines (79 loc) · 2.05 KB
/
03p1.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
package main
import (
"fmt"
"strconv"
"aoc2023/utils"
)
type enginePart struct {
number int
isPart bool
partIdentifier rune
identifierPosition coordinate
}
type coordinate struct {
x int
y int
}
func D03P1() {
lines := utils.ReadLines("inputs/03.txt")
grid := make([][]rune, len(lines))
engineParts := []enginePart{}
for i, line := range lines {
grid[i] = []rune(line)
}
currentPart := enginePart{}
partNumberString := ""
for i, line := range grid {
for j, char := range line {
if char >= '0' && char <= '9' {
partNumberString += string(char)
if !currentPart.isPart {
checkIfPartIsPart(coordinate{j, i}, grid, ¤tPart)
}
} else if partNumberString != "" {
currentPart.number, _ = strconv.Atoi(partNumberString)
engineParts = append(engineParts, currentPart)
partNumberString = ""
currentPart = enginePart{}
}
}
}
sumOfParts := 0
for _, part := range engineParts {
if part.isPart {
sumOfParts += part.number
}
}
fmt.Printf("Sum of parts: %d\n", sumOfParts)
}
func checkIfPartIsPart(position coordinate, grid [][]rune, currentPart *enginePart) {
neighbouringGridPoisitions := []coordinate{
{position.x + 1, position.y + 1},
{position.x + 1, position.y - 1},
{position.x + 1, position.y},
{position.x - 1, position.y + 1},
{position.x - 1, position.y - 1},
{position.x - 1, position.y},
{position.x, position.y + 1},
{position.x, position.y - 1},
}
for _, neighbour := range neighbouringGridPoisitions {
//Check if coordinate is in grid
if neighbour.x < 0 || neighbour.y < 0 || neighbour.x >= len(grid) ||
neighbour.y >= len(grid[0]) {
continue
}
// Check if the neighbour is a number
if grid[neighbour.y][neighbour.x] >= '0' && grid[neighbour.y][neighbour.x] <= '9' {
continue
}
// Check if the neighbour is a full stop
if grid[neighbour.y][neighbour.x] == '.' {
continue
}
currentPart.partIdentifier = grid[neighbour.y][neighbour.x]
currentPart.identifierPosition = neighbour
currentPart.isPart = true
return
}
}