-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday05.go
More file actions
170 lines (151 loc) · 3.98 KB
/
day05.go
File metadata and controls
170 lines (151 loc) · 3.98 KB
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
package main
import (
"AdventOfCode-go/advent2021/utils"
"fmt"
)
type coords struct {
x int
y int
}
func calcOverlapPoints(ventLines []string, part byte, debug bool) int {
areaDiagram := make(map[coords]int)
var startCoords, endCoords coords
for i := 0; i < len(ventLines); i++ {
fmt.Sscanf(ventLines[i], "%d,%d -> %d,%d\n", &startCoords.x, &startCoords.y, &endCoords.x, &endCoords.y)
if debug {
fmt.Println("Input Line:", ventLines[i])
fmt.Printf("Start x:%d, Start y:%d, End x:%d, End y:%d\n",
startCoords.x, startCoords.y, endCoords.x, endCoords.y)
}
// In part a we only care about vertical or horizontal lines. Check for those
if part == 'a' {
if startCoords.x == endCoords.x {
var loopStart, loopEnd int
if startCoords.y > endCoords.y {
loopStart = endCoords.y
loopEnd = startCoords.y
} else {
loopStart = startCoords.y
loopEnd = endCoords.y
}
for i := loopStart; i <= loopEnd; i++ {
if debug {
fmt.Printf("x:%d y:%d\n", startCoords.x, i)
}
areaDiagram[coords{startCoords.x, i}]++
}
}
if startCoords.y == endCoords.y {
var loopStart, loopEnd int
if startCoords.x > endCoords.x {
loopStart = endCoords.x
loopEnd = startCoords.x
} else {
loopStart = startCoords.x
loopEnd = endCoords.x
}
for i := loopStart; i <= loopEnd; i++ {
if debug {
fmt.Printf("x:%d y:%d\n", i, startCoords.y)
}
areaDiagram[coords{i, startCoords.y}]++
}
}
if debug {
fmt.Println(areaDiagram)
}
} else {
if startCoords.x == endCoords.x {
var loopStart, loopEnd int
if startCoords.y > endCoords.y {
loopStart = endCoords.y
loopEnd = startCoords.y
} else {
loopStart = startCoords.y
loopEnd = endCoords.y
}
for i := loopStart; i <= loopEnd; i++ {
if debug {
fmt.Printf("x:%d y:%d\n", startCoords.x, i)
}
areaDiagram[coords{startCoords.x, i}]++
}
}
if startCoords.y == endCoords.y {
var loopStart, loopEnd int
if startCoords.x > endCoords.x {
loopStart = endCoords.x
loopEnd = startCoords.x
} else {
loopStart = startCoords.x
loopEnd = endCoords.x
}
for i := loopStart; i <= loopEnd; i++ {
if debug {
fmt.Printf("x:%d y:%d\n", i, startCoords.y)
}
areaDiagram[coords{i, startCoords.y}]++
}
}
// Part b also has diagonals at 45 degrees. Lets check for them
if (startCoords.x != endCoords.x) && (startCoords.y != endCoords.y) {
var loopStartX, loopEndX, loopStartY, loopEndY int
var backwards bool = false
if startCoords.x > endCoords.x {
loopStartX = endCoords.x
loopEndX = startCoords.x
loopStartY = endCoords.y
loopEndY = startCoords.y
// need to deal with backwards diagonal where y decreases
if loopStartY > loopEndY {
backwards = true
}
} else {
loopStartX = startCoords.x
loopEndX = endCoords.x
loopStartY = startCoords.y
loopEndY = endCoords.y
// need to deal with backwards diagonal where y decreases
if loopStartY > loopEndY {
backwards = true
}
}
var currentY = loopStartY
for currentX := loopStartX; currentX <= loopEndX; currentX++ {
if debug {
fmt.Printf("x:%d y:%d\n", currentX, currentY)
}
areaDiagram[coords{currentX, currentY}]++
if backwards {
currentY--
} else {
currentY++
}
}
}
if debug {
fmt.Println(areaDiagram)
}
}
}
var overlapPoints int
for _, i := range areaDiagram {
if i > 1 {
overlapPoints++
}
}
return overlapPoints
}
func solveDay(filename string, part byte, debug bool) int {
puzzleInput, _ := utils.ReadFile(filename)
return calcOverlapPoints(puzzleInput, part, debug)
}
// Main routine
func main() {
filenamePtr, execPart, debug := utils.CatchUserInput()
if execPart == 'z' {
fmt.Println("Bad part choice. Available choices are 'a' and 'b'")
} else {
fmt.Printf("Result is: %d\n", solveDay(filenamePtr, execPart, debug))
}
}