-
Notifications
You must be signed in to change notification settings - Fork 0
/
03p2.go
64 lines (50 loc) · 1.42 KB
/
03p2.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
package main
import (
"fmt"
"strconv"
"aoc2023/utils"
)
func D03P2() {
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{}
}
}
}
// Using a map prevents duplicate gear ratios
gearRatios := map[coordinate]int{}
for _, part := range engineParts {
if part.partIdentifier == '*' {
// Check if the other half of the gear ratio is in the list
for _, otherPart := range engineParts {
if otherPart.partIdentifier == '*' &&
otherPart.identifierPosition == part.identifierPosition && otherPart.number != part.number {
// Add the gear ratio to the map
gearRatios[part.identifierPosition] = part.number * otherPart.number
}
}
}
}
totalSumOfGearRatios := 0
for _, gearRatio := range gearRatios {
totalSumOfGearRatios += gearRatio
}
fmt.Printf("Sum of gear ratios: %d\n", totalSumOfGearRatios)
}