-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
187 lines (163 loc) · 4.01 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
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
package main
import (
"fmt"
aoc "github.com/shraddhaag/aoc/library"
)
func main() {
input := aoc.Get2DGrid(aoc.ReadFileLineByLine("input.txt"))
ans1, path := findPath(input, findStartingPoint(input))
ans2 := findNewObstacleCount(input, findStartingPoint(input), path)
fmt.Println("answer for part 1: ", ans1)
fmt.Println("answer for part 2: ", ans2)
}
func findStartingPoint(input [][]string) point {
for i, row := range input {
for j, char := range row {
switch char {
case "^":
return point{j, i, up}
case "<":
return point{j, i, left}
case ">":
return point{j, i, right}
case "v":
return point{j, i, down}
}
}
}
return point{-1, -1, up}
}
type point struct {
x int
y int
direction int
}
type coordinates struct {
x int
y int
}
const (
up = 0
down = 1
right = 2
left = 3
)
func findPath(input [][]string, start point) (int, map[coordinates]int) {
path := make(map[coordinates]int)
count := 0
current := start
for {
if _, ok := path[coordinates{current.x, current.y}]; !ok {
count++
path[coordinates{current.x, current.y}] = current.direction
}
isValid, newCurrent := findNextStep(input, current)
if !isValid {
return count, path
}
current = newCurrent
}
return count, path
}
// main thing to note: we enounter a loop whenever we come
// across the same coordinates + direction.
func isLoop(input [][]string, start point) bool {
path := make(map[point]struct{})
path2 := make(map[coordinates]struct{})
current := start
for {
// update path
if _, ok := path[current]; !ok {
path[current] = struct{}{}
} else {
return true
}
if _, ok := path2[coordinates{current.x, current.y}]; !ok {
path2[coordinates{current.x, current.y}] = struct{}{}
}
valid, newCurrent := findNextStep(input, current)
if !valid {
return false
}
current = newCurrent
}
return false
}
func findNextStep(input [][]string, current point) (bool, point) {
valid, possibleNext := getNextStepWithDirectionPreserved(input, current)
if !valid {
return false, possibleNext
}
switch input[possibleNext.y][possibleNext.x] {
case "#":
// this is really subtle, consider the below case
// where > represents your current position + direction:
// ....#.....
// ........>#
// ........#.
// When you turn right and step, you again encounter
// an obstacle (a '#'). This is a valid case and you
// can not exit the loop at this point.
// Instead, you turn right once MORE, an effective turn of
// 180 degrees this time, and then continue forward.
return findNextStep(input, turn90(input, current))
case ".":
return true, possibleNext
case "^":
return true, possibleNext
}
return false, possibleNext
}
// for the guard to be stuck in a loop, the new obstacle has to
// be placed on the guard's existing path (ie path figured out
// in the first part).
// obstacle placed at any other place will not change the guard's path.
func findNewObstacleCount(input [][]string, start point, path map[coordinates]int) int {
count := 0
obstanceMap := make(map[coordinates]struct{})
for step, _ := range path {
if step.x == start.x && step.y == start.y {
continue
}
if input[step.y][step.x] == "." {
input[step.y][step.x] = "#"
if isLoop(input, start) {
if _, ok := obstanceMap[step]; !ok {
count++
obstanceMap[step] = struct{}{}
}
}
input[step.y][step.x] = "."
}
}
return count
}
func getNextStepWithDirectionPreserved(input [][]string, current point) (bool, point) {
switch current.direction {
case up:
current.y -= 1
case down:
current.y += 1
case right:
current.x += 1
case left:
current.x -= 1
}
if current.x < 0 || current.y < 0 || current.x >= len(input[0]) || current.y >= len(input) {
return false, current
}
return true, current
}
func turn90(input [][]string, current point) point {
switch current.direction {
case up:
current.direction = right
case down:
current.direction = left
case right:
current.direction = down
case left:
current.direction = up
}
return current
}