diff options
author | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2023-01-27 16:19:57 +0100 |
---|---|---|
committer | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2023-01-27 16:19:57 +0100 |
commit | 96d397d8e184606315ef0e08273cea4f3fcd3f5d (patch) | |
tree | 2c52afd9b71a4025bcb7a9a554ba9098b0453f71 /aoc-2020-gleam/src/util | |
parent | 29c49417f89169c03ca05724da3c88880d3a1a17 (diff) | |
download | gleam_aoc2020-96d397d8e184606315ef0e08273cea4f3fcd3f5d.tar.gz gleam_aoc2020-96d397d8e184606315ef0e08273cea4f3fcd3f5d.zip |
Solve day 1 of 2020 in Gleam
Diffstat (limited to 'aoc-2020-gleam/src/util')
-rw-r--r-- | aoc-2020-gleam/src/util/input_util.gleam | 26 | ||||
-rw-r--r-- | aoc-2020-gleam/src/util/runner.gleam | 32 |
2 files changed, 58 insertions, 0 deletions
diff --git a/aoc-2020-gleam/src/util/input_util.gleam b/aoc-2020-gleam/src/util/input_util.gleam new file mode 100644 index 0000000..3e6b076 --- /dev/null +++ b/aoc-2020-gleam/src/util/input_util.gleam @@ -0,0 +1,26 @@ +import gleam/list +import gleam/string +import gleam/erlang/file +import ext/intx +import ext/stringx +import ext/resultx + +pub fn read_text(filename: String) -> String { + "data/" <> filename <> ".txt" + |> file.read() + |> resultx.force_unwrap() +} + +pub fn read_lines(filename: String) -> List(String) { + filename + |> read_text() + |> string.split(on: "\n") + |> list.map(with: string.trim) + |> list.filter(for: stringx.is_not_empty) +} + +pub fn read_numbers(filename: String) -> List(Int) { + filename + |> read_lines() + |> list.map(with: intx.force_parse) +} diff --git a/aoc-2020-gleam/src/util/runner.gleam b/aoc-2020-gleam/src/util/runner.gleam new file mode 100644 index 0000000..0ff0a41 --- /dev/null +++ b/aoc-2020-gleam/src/util/runner.gleam @@ -0,0 +1,32 @@ +import gleam/list +import gleam/int +import gleam/io +import gleam/result +import gleam/erlang.{start_arguments} + +fn get_day(handler: fn(Int) -> Nil) -> Result(Nil, String) { + let args = start_arguments() + + use first <- result.then( + args + |> list.first() + |> result.replace_error("Pass the day as first argument!"), + ) + + use day <- result.then( + first + |> int.parse() + |> result.replace_error("The day argument must be a number!"), + ) + + handler(day) + + Ok(Nil) +} + +pub fn with_day(handler: fn(Int) -> Nil) -> Nil { + handler + |> get_day() + |> result.map_error(io.println) + |> result.unwrap(Nil) +} |