-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday23.go
190 lines (160 loc) · 4.75 KB
/
day23.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
188
189
190
package main
import (
"fmt"
"github.com/Alex-Waring/AoC/utils"
)
type Entry struct {
pos utils.Position
history []utils.Position
}
func getPossibleMoves(grid map[utils.Position]string, pos utils.Position, max_row int, max_col int) []utils.Position {
directions := []utils.Direction{utils.Up, utils.Down, utils.Left, utils.Right}
return_list := []utils.Position{}
switch grid[pos] {
case ">":
new_pos := pos.Move(utils.Right, 1)
if char, exists := grid[new_pos]; exists && char != "#" {
return_list = append(return_list, new_pos)
}
case "v":
new_pos := pos.Move(utils.Down, 1)
if char, exists := grid[new_pos]; exists && char != "#" {
return_list = append(return_list, new_pos)
}
case ".":
for _, direction := range directions {
new_pos := pos.Move(direction, 1)
if char, exists := grid[new_pos]; exists && char != "#" {
return_list = append(return_list, new_pos)
}
}
}
return return_list
}
func getPossibleMovesPart2(grid map[utils.Position]string, pos utils.Position, max_row int, max_col int) []utils.Position {
directions := []utils.Direction{utils.Up, utils.Down, utils.Left, utils.Right}
return_list := []utils.Position{}
for _, direction := range directions {
new_pos := pos.Move(direction, 1)
if char, exists := grid[new_pos]; exists && char != "#" {
return_list = append(return_list, new_pos)
}
}
return return_list
}
func drawMap(grid map[utils.Position]string, history []utils.Position, max_row int, max_col int) {
for row := 0; row <= max_row; row++ {
for col := 0; col <= max_col; col++ {
pos := utils.NewPosition(row, col)
loc := grid[pos]
if utils.PosInSlice(pos, history) {
fmt.Print("O")
} else {
fmt.Print(loc)
}
}
fmt.Println()
}
fmt.Println()
}
func part1(grid map[utils.Position]string, finish utils.Position, max_row int, max_col int) {
defer utils.Timer("part1")()
finished := []int{}
q := utils.Queue[Entry]{}
q.Push(Entry{
pos: utils.Position{Row: 0, Col: 1},
history: []utils.Position{},
})
for !q.IsEmpty() {
entry := q.Pop()
// If we're at the finish, add the number of steps to finished
if entry.pos == finish {
finished = append(finished, len(entry.history))
continue
}
// Get the possible moves
next_moves := getPossibleMoves(grid, entry.pos, max_row, max_col)
// For each possible move, if we've been there in the history discard
// If we havn't, create a new entry with it in
for _, move := range next_moves {
if !utils.PosInSlice(move, entry.history) {
q.Push(Entry{
pos: move,
history: append(entry.history, entry.pos),
})
// drawMap(grid, append(entry.history, entry.pos), max_row, max_col)
}
}
}
fmt.Println(utils.FindMax(finished))
}
func findNextJunction(grid map[utils.Position]string, entry Entry, max_row int, max_col int, finish utils.Position) []Entry {
return_list := []Entry{}
if entry.pos == finish {
return_list = append(return_list, entry)
return return_list
}
// Get the possible moves
next_moves := getPossibleMovesPart2(grid, entry.pos, max_row, max_col)
// If we have two possible moves, add the one that isn't in the history to the
// entry and continue
if len(next_moves) == 2 {
for _, move := range next_moves {
if !utils.PosInSlice(move, entry.history) {
new_entry := Entry{
history: append(entry.history, entry.pos),
pos: move,
}
return findNextJunction(grid, new_entry, max_row, max_col, finish)
}
}
} else {
// Otherwise do the normal loop and try and solve it
for _, move := range next_moves {
if !utils.PosInSlice(move, entry.history) {
return_list = append(return_list, Entry{
pos: move,
history: append(entry.history, entry.pos),
})
}
}
}
return return_list
}
func part2(grid map[utils.Position]string, finish utils.Position, max_row int, max_col int) {
defer utils.Timer("part2")()
finished := []int{}
q := utils.Queue[Entry]{}
q.Push(Entry{
pos: utils.Position{Row: 0, Col: 1},
history: []utils.Position{},
})
for !q.IsEmpty() {
entry := q.Pop()
// If we're at the finish, add the number of steps to finished
if entry.pos == finish {
finished = append(finished, len(entry.history))
continue
}
next_moves := findNextJunction(grid, entry, max_row, max_col, finish)
for _, next := range next_moves {
q.Push(next)
}
}
fmt.Println(utils.FindMax(finished))
}
func main() {
lines := utils.ReadInput("input.txt")
grid := map[utils.Position]string{}
max_row := len(lines) - 1
max_col := len(lines[0]) - 1
for row, line := range lines {
for col, char := range line {
new_pos := utils.NewPosition(row, col)
grid[new_pos] = string(char)
}
}
finish := utils.NewPosition(max_row, max_col-1)
part1(grid, finish, max_row, max_col)
part2(grid, finish, max_row, max_col)
}