-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday06.go
139 lines (127 loc) · 3.61 KB
/
day06.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
package main
import (
"bufio"
"fmt"
"os"
)
type Position struct {
y, x int
}
func main() {
file, err := os.Open("day06/day06.txt")
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer file.Close()
mapGrid := [][]byte{}
var startFound bool
var startPos Position
var y int
scanner := bufio.NewScanner(file)
for scanner.Scan() {
row := []byte(scanner.Text())
mapGrid = append(mapGrid, row)
if !startFound {
for x, char := range row {
if char == '^' || char == '>' || char == 'V' || char == '<' {
startPos = Position{y, x}
startFound = true
break
}
}
}
y++
}
if err := scanner.Err(); err != nil {
fmt.Println("Error reading file:", err)
return
}
if !startFound {
fmt.Println("Unable to find starting position in input")
return
}
var visitedPos, _ = getVisitedPositions(mapGrid, startPos)
var part1Count int = len(visitedPos)
var part2Count int = getObstacleInfiniteLoopCount(mapGrid, startPos, visitedPos)
fmt.Println("Part 1 - Number of locations visited by guard:", part1Count)
fmt.Println("Part 2 - Number of infinite loop obstacle locations:", part2Count)
}
func newGrid(mapGrid [][]byte) [][]byte {
newGrid := make([][]byte, len(mapGrid))
for i := range mapGrid {
newGrid[i] = make([]byte, len(mapGrid[i]))
copy(newGrid[i], mapGrid[i])
}
return newGrid
}
func getObstacleInfiniteLoopCount(mapGrid [][]byte, startPos Position, visitedPos map[Position]bool) int {
var obstacleCount int
// Remove the starting position from the list of visited places we can test an obstacle at
delete(visitedPos, Position{startPos.y, startPos.x})
for pos := range visitedPos{
testGrid := newGrid(mapGrid)
testGrid[pos.y][pos.x] = '#'
var _, isALoop = getVisitedPositions(testGrid, startPos)
if isALoop {
obstacleCount++
}
}
return obstacleCount
}
func getVisitedPositions(mapGrid [][]byte, currentPos Position) (map[Position]bool, bool) {
directions := map[byte]Position{
'^': {-1, 0},
'>': {0, 1},
'V': {1, 0},
'<': {0, -1},
}
turnOrder := []byte{'^', '>', 'V', '<'}
currentDir := mapGrid[currentPos.y][currentPos.x]
visitedPos := make(map[Position]bool)
obstaclesHit := make(map[Position]byte)
isALoop := false
for {
visitedPos[currentPos] = true
nextPos := Position{currentPos.y + directions[currentDir].y, currentPos.x + directions[currentDir].x}
if nextPos.y < 0 || nextPos.y >= len(mapGrid) || nextPos.x < 0 || nextPos.x >= len(mapGrid[0]) {
break
}
if mapGrid[nextPos.y][nextPos.x] == '#' {
if obstaclesHit[nextPos] == currentDir {
isALoop = true
return visitedPos, isALoop
} else {
obstaclesHit[nextPos] = currentDir
}
for i, dir := range turnOrder {
if dir == currentDir {
currentDir = turnOrder[(i+1)%4]
break
}
}
continue
}
currentPos = nextPos
}
return visitedPos, isALoop
}
// Part 1 Notes
// ------------------------
// Map ^>V< to direction vectors
// Create map of visited coords
// Read map from txt file
// Find starting point (contains ^>V<)
// While not out of bounds
// Mark current location as visited
// Add to count
// Check in direction ahead for obstacle "#"
// If not, move forward to next location
// Else change direction to next in map
// Part 2 Notes
// ------------------------
// Only need to check locations where you initially walked
// Store location and direction of encounter with each obstacle
// Generate and test a new mapGrid with the new obstacle in place
// If you ever encounter an obstacle from the same direction, you must be stuck in a loop
// Therefore increment the loop counter and test the next location