-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc23.gleam
329 lines (295 loc) · 7.18 KB
/
aoc23.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
//// this has been fun but doesnt work right now
import gleam/io
import gleam/string
import gleam/int
import gleam/list
import gleam/option.{type Option, None, Some}
import gleam/result.{try}
import gleam/dict.{type Dict}
import lib/range.{type Range}
import argv
pub type FileError {
Enoent
}
@external(erlang, "file", "read_file")
fn read_file(path: String) -> Result(String, FileError)
pub type Part {
A
B
}
pub fn main() {
let args = argv.load().arguments
let part = case args {
[part] -> {
case part {
"1" | "a" | "A" -> A
"2" | "b" | "B" -> B
_ -> panic as "Invalid part"
}
}
_ -> panic as "No part provided"
}
let either_content = read_file("./input/test/day5.txt")
case either_content, part {
Ok(content), part -> {
let #(seeds, Input(map_to_list)) = parse(content)
let lowest_num = resolve(part, seeds, map_to_list)
io.println("Lowest number is: " <> min_to_string(lowest_num))
}
Error(_), _ -> io.println("Error reading file")
}
}
const oh_no = [
#("seed", "soil"),
#("soil", "fertilizer"),
#("fertilizer", "water"),
#("water", "light"),
#("light", "temperature"),
#("temperature", "humidity"),
#("humidity", "location"),
]
type Min {
Inf
Num(Int)
}
fn min(m: Min, b: Int) {
case m {
Inf -> Num(b)
Num(a) -> Num(int.min(a, b))
}
}
fn min_to_string(min: Min) -> String {
case min {
Inf -> "Inf"
Num(int) -> int.to_string(int)
}
}
fn resolve(part, seeds, map: Dict(#(String, String), List(Mapper))) -> Min {
case part {
A -> resolve_a(seeds, map)
B -> resolve_b(seeds, map)
}
}
fn resolve_a(seeds, map) -> Min {
use total, seed <- list.fold(seeds, Inf)
let new = {
use last_seed, key <- list.fold(oh_no, seed)
// advent of code guarantees that the key is in the map
let assert Ok(maps) = dict.get(map, key)
use last, map <- list.fold(maps, last_seed)
// either gets a new value or returns the same value unchanged
mapped_value(map, last_seed)
|> option.unwrap(last)
}
case total {
Inf -> new
Num(int) -> int.min(int, new)
}
|> Num
}
fn resolve_b(seeds, map) {
inner_b(seeds, map)
|> min_rs
}
fn min_rs(ranges: List(Range)) -> Min {
use acc, value <- list.fold(ranges, Inf)
let #(from, to) = range.bounds(value)
min(acc, from)
|> min(to)
}
fn inner_b(seeds, map) -> List(Range) {
// if its not none there is something wrong
let assert #(b_seeds, None) =
list.fold(seeds, #([], None), fn(acc, seed) {
case acc {
#(xs, None) -> #(xs, Some(seed))
#(xs, Some(value)) -> #(
[range.new_left(value, seed + value), ..xs],
None,
)
}
})
let input =
[
list.at(
b_seeds
|> list.reverse,
0,
)
|> result.unwrap(range.singleton(-1_202_303_404)),
]
|> io.debug
use seed_acc, seed_value <- list.fold(input, [])
let ranges = map_seed(seed_value, map)
list.append(ranges, seed_acc)
}
/// returns the new ranges after they've been mapped
fn map_seed(
seed: Range,
map: Dict(#(String, String), List(Mapper)),
) -> List(Range) {
use acc, key <- list.fold(oh_no, [seed])
let assert Ok(maps) = dict.get(map, key)
io.debug(#(key, maps, acc))
let foo = {
use value <- list.flat_map(acc)
io.debug(#("calling resolve_maps with", value))
resolve_maps(maps, value)
}
io.debug(#("foo:", foo))
io.debug("---")
foo
}
fn resolve_maps(maps: List(Mapper), seed: Range) -> List(Range) {
use acc, map <- list.fold(maps, [seed])
io.debug(#("iniside resolve_maps", "acc", acc, "map", map))
use value <- list.flat_map(acc)
io.debug(#("using value", value))
let ps =
parts(seed, map)
|> result.unwrap([])
|> io.debug
|> list.map(mapped_range(map, _))
case ps == [] {
True -> [value]
False -> ps
}
}
pub fn parts(range: Range, map: Mapper) -> range.PartsResult {
let map_range = range.new_left(map.src, map.src + map.range)
case range.overlap(range, map_range) {
range.BothOverlap -> {
range.parts(
range,
map_range
|> range.map_to(fn(x) { x + 1 }),
)
}
_ -> range.parts(range, map_range)
}
}
pub type Mapper {
Map(dst: Int, src: Int, range: Int)
}
pub fn mapped_range(mapper: Mapper, r: Range) -> Range {
let #(from, to) = range.bounds(r)
let from1 =
mapped_value(mapper, from)
|> option.unwrap(from)
let to1 =
mapped_value(mapper, to)
|> option.unwrap(to)
range.new_both(from1, to1)
}
pub fn mapped_value(mapper: Mapper, value: Int) -> Option(Int) {
let Map(dst, src, range) = mapper
case value >= src && value <= src + range - 1 {
True -> {
let diff =
src - value
|> int.absolute_value
dst + diff
|> Some
}
False -> None
}
}
pub type Input {
Input(maps: Dict(#(String, String), List(Mapper)))
}
type Seed =
Int
pub fn parse(content: String) -> #(List(Seed), Input) {
let lines = string.split(content, "\n")
let assert Some(seeds) =
list.at(lines, 0)
|> result.unwrap("")
|> parse_seed
let maps =
list.drop(lines, 1)
|> parse_maps_and_values
#(seeds, maps)
}
type Go {
Go(key: Option(#(From, To)), lst: Input)
}
fn parse_maps_and_values(lines) -> Input {
let Go(_, in) = helper_parse_maps_and_values(lines)
in
}
// how to NOT do it
fn helper_parse_maps_and_values(lines) -> Go {
use acc, line <- list.fold(lines, Go(None, Input(maps: dict.new())))
let maybe_map_name = parse_map_name(line)
let maybe_mapper = parse_mapper(line)
case maybe_map_name, maybe_mapper {
Some(_) as some, None -> {
let Go(_, input) = acc
Go(some, input)
}
None, Some(mapper) -> {
let Go(maybe_key, input) = acc
case maybe_key {
None -> acc
Some(key) -> {
let new_input = {
use maybe_value <- dict.update(input.maps, key)
case maybe_value {
None -> [mapper]
Some(value) -> list.append(value, [mapper])
}
}
Go(maybe_key, Input(maps: new_input))
}
}
}
_, _ -> acc
}
}
pub fn parse_seed(content: String) -> Option(List(Seed)) {
case string.contains(content, "seeds: ") {
False -> None
True -> {
let seed =
string.split(content, "seeds: ")
|> list.drop(1)
|> list.first
let ret = {
use str <- result.map(seed)
let seeds = string.split(str, " ")
use seed <- list.filter_map(seeds)
int.parse(seed)
}
option.from_result(ret)
}
}
}
type From =
String
type To =
String
pub fn parse_map_name(content: String) -> Option(#(From, To)) {
let parts = string.split(content, "-")
case parts {
[from, "to", rest] -> {
let to = string.drop_right(rest, string.length(" map:"))
Some(#(from, to))
}
_ -> None
}
}
pub fn parse_mapper(content: String) -> Option(Mapper) {
let parts = string.split(content, " ")
case parts {
[dst, src, by] -> {
let ret = {
use destination <- try(int.parse(dst))
use souce <- try(int.parse(src))
use range <- result.map(int.parse(by))
Map(destination, souce, range)
}
option.from_result(ret)
}
_ -> None
}
}