-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday16.gleam
185 lines (172 loc) · 4.5 KB
/
day16.gleam
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
import coord.{type Coord}
import gleam/dict.{type Dict}
import gleam/int
import gleam/io
import gleam/list
import gleam/order
import gleam/pair
import gleam/set
import gleam/string
import pretty
import util
type Step {
Step(
position: Coord,
direction: Coord,
score: Int,
estimate: Int,
path: List(#(Coord, Coord)),
)
}
pub type MazeResult {
MazeResult(score: Int, paths: List(List(#(Coord, Coord))))
}
pub fn run(input) {
let maze = util.as_grid(input)
let assert Ok(MazeResult(part1_score, paths)) = part1(maze)
pretty.part1_result_int(part1_score)
let part2 =
paths
|> list.flatten()
|> list.map(pair.first)
|> set.from_list()
|> set.size
pretty.part2_result_int(part2)
}
pub fn part1(maze: Dict(Coord, String)) {
let assert [start, end] =
dict.filter(maze, fn(_, v) { v == "S" || v == "E" })
|> dict.to_list()
|> list.sort(fn(a, b) { string.compare(a.1, b.1) |> order.negate() })
|> list.map(pair.first)
find_shortest(maze, start, end)
}
fn find_shortest(maze, start, end) {
let estimate = coord.manhattan_dist(start, end)
let start_step = Step(start, coord.east, 0, estimate, [#(start, coord.east)])
do_find_shortest(
maze,
end,
Error(Nil),
[start_step],
add_step(dict.new(), start_step),
)
}
fn do_find_shortest(maze, target, maze_result, to_visit, visited) {
case to_visit {
[] -> maze_result
[Step(pos, _, score, _, path) as step, ..rest] -> {
case pos == target, maze_result {
True, Error(_) ->
do_find_shortest(
maze,
target,
Ok(MazeResult(score, [path])),
rest,
visited,
)
True, Ok(MazeResult(s, paths)) if score == s -> {
do_find_shortest(
maze,
target,
Ok(MazeResult(s, [path, ..paths])),
rest,
visited,
)
}
True, Ok(_) -> maze_result
False, _ -> {
let possible =
next_moves(step, target)
|> list.filter(fn(s) {
case dict.get(maze, s.position) {
Error(Nil) | Ok("#") -> False
_ -> True
}
})
|> list.filter(fn(s) {
case dict.get(visited, #(s.position, s.direction)) {
Error(Nil) -> True
Ok(score) if s.score <= score -> True
_ -> False
}
})
let new_to_visit =
list.append(possible, rest)
|> list.sort(shortest_step)
let new_visited = list.fold(possible, visited, add_step)
do_find_shortest(maze, target, maze_result, new_to_visit, new_visited)
}
}
}
}
}
fn add_step(visited, step: Step) {
dict.insert(visited, #(step.position, step.direction), step.score)
}
fn shortest_step(a: Step, b: Step) {
int.compare(a.score + a.estimate, b.score + b.estimate)
}
fn next_moves(from: Step, target: Coord) {
let next_pos = coord.plus(from.position, from.direction)
let straight =
Step(
next_pos,
from.direction,
from.score + 1,
coord.manhattan_dist(next_pos, target),
[#(next_pos, from.direction), ..from.path],
)
[turn_clockwise(from.direction), turn_counterclockwise(from.direction)]
|> list.map(fn(dir) {
Step(from.position, dir, from.score + 1000, from.estimate, from.path)
})
|> list.prepend(straight)
}
fn turn_clockwise(dir: Coord) {
case dir {
#(0, 1) -> #(-1, 0)
#(-1, 0) -> #(0, -1)
#(0, -1) -> #(1, 0)
#(1, 0) -> #(0, 1)
d -> panic as { "Unknown direction " <> coord.to_string(d) }
}
}
fn turn_counterclockwise(dir: Coord) {
case dir {
#(0, 1) -> #(1, 0)
#(1, 0) -> #(0, -1)
#(0, -1) -> #(-1, 0)
#(-1, 0) -> #(0, 1)
d -> panic as { "Unknown direction " <> coord.to_string(d) }
}
}
pub fn show(map, path) {
let path_dict = dict.from_list(path)
let assert Ok(#(xmax, ymax)) = dict.keys(map) |> list.reduce(coord.max)
list.range(0, ymax)
|> list.map(fn(y) {
list.range(0, xmax)
|> list.map(fn(x) {
let pos = #(x, y)
case dict.get(path_dict, pos), dict.get(map, pos) {
Ok(c), _ -> show_dir(c)
_, Ok(m) -> m
_, Error(_) -> panic as { "Outside map" }
}
})
|> string.join("")
})
|> string.join("\n")
|> io.println()
io.println("")
}
fn show_dir(c) {
case c {
#(0, 1) -> "v"
#(-1, 0) -> "<"
#(0, -1) -> "^"
#(1, 0) -> ">"
d -> panic as { "Unknown direction " <> coord.to_string(d) }
}
}