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 | |
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')
-rw-r--r-- | aoc-2020-gleam/gleam.toml | 7 | ||||
-rw-r--r-- | aoc-2020-gleam/manifest.toml | 11 | ||||
-rw-r--r-- | aoc-2020-gleam/src/aoc_2020_gleam.gleam | 11 | ||||
-rw-r--r-- | aoc-2020-gleam/src/days/day01.gleam | 34 | ||||
-rw-r--r-- | aoc-2020-gleam/src/ext/intx.gleam | 8 | ||||
-rw-r--r-- | aoc-2020-gleam/src/ext/resultx.gleam | 4 | ||||
-rw-r--r-- | aoc-2020-gleam/src/ext/stringx.gleam | 5 | ||||
-rw-r--r-- | aoc-2020-gleam/src/util/input_util.gleam | 26 | ||||
-rw-r--r-- | aoc-2020-gleam/src/util/runner.gleam | 32 |
9 files changed, 138 insertions, 0 deletions
diff --git a/aoc-2020-gleam/gleam.toml b/aoc-2020-gleam/gleam.toml new file mode 100644 index 0000000..cf6aa37 --- /dev/null +++ b/aoc-2020-gleam/gleam.toml @@ -0,0 +1,7 @@ +name = "aoc_2020_gleam" +version = "0.1.0" +description = "A Gleam project" + +[dependencies] +gleam_stdlib = "~> 0.26" +gleam_erlang = "~> 0.17" diff --git a/aoc-2020-gleam/manifest.toml b/aoc-2020-gleam/manifest.toml new file mode 100644 index 0000000..b1d3141 --- /dev/null +++ b/aoc-2020-gleam/manifest.toml @@ -0,0 +1,11 @@ +# This file was generated by Gleam +# You typically do not need to edit this file + +packages = [ + { name = "gleam_erlang", version = "0.17.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "BAAA84F5BCC4477E809BA3E03BB3009A3894A6544C1511626C44408E39DB2AE6" }, + { name = "gleam_stdlib", version = "0.26.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "6221F9D7A08B6D6DBCDD567B2BB7C4B2A7BBF4C04C6110757BE04635143BDEC8" }, +] + +[requirements] +gleam_erlang = "~> 0.17" +gleam_stdlib = "~> 0.26" diff --git a/aoc-2020-gleam/src/aoc_2020_gleam.gleam b/aoc-2020-gleam/src/aoc_2020_gleam.gleam new file mode 100644 index 0000000..e27910d --- /dev/null +++ b/aoc-2020-gleam/src/aoc_2020_gleam.gleam @@ -0,0 +1,11 @@ +import gleam/io +import util/runner +import days/day01 + +pub fn main() -> Nil { + use day <- runner.with_day() + case day { + 1 -> day01.run() + _ -> io.println("Day not found!") + } +} diff --git a/aoc-2020-gleam/src/days/day01.gleam b/aoc-2020-gleam/src/days/day01.gleam new file mode 100644 index 0000000..6e75826 --- /dev/null +++ b/aoc-2020-gleam/src/days/day01.gleam @@ -0,0 +1,34 @@ +import gleam/io +import gleam/int +import gleam/list +import gleam/result +import ext/resultx +import util/input_util + +fn solve(numbers: List(Int), n: Int) -> Int { + numbers + |> list.combinations(by: n) + |> list.find(one_that: fn(p) { int.sum(p) == 2020 }) + |> result.map(with: int.product) + |> resultx.force_unwrap() +} + +fn part1(numbers: List(Int)) -> Int { + solve(numbers, 2) +} + +fn part2(numbers: List(Int)) -> Int { + solve(numbers, 3) +} + +pub fn run() -> Nil { + let test = input_util.read_numbers("test01") + assert 514_579 = part1(test) + assert 241_861_950 = part2(test) + + let input = input_util.read_numbers("day01") + io.debug(part1(input)) + io.debug(part2(input)) + + Nil +} diff --git a/aoc-2020-gleam/src/ext/intx.gleam b/aoc-2020-gleam/src/ext/intx.gleam new file mode 100644 index 0000000..3cfe144 --- /dev/null +++ b/aoc-2020-gleam/src/ext/intx.gleam @@ -0,0 +1,8 @@ +import gleam/int +import ext/resultx + +pub fn force_parse(string: String) -> Int { + string + |> int.parse() + |> resultx.force_unwrap() +} diff --git a/aoc-2020-gleam/src/ext/resultx.gleam b/aoc-2020-gleam/src/ext/resultx.gleam new file mode 100644 index 0000000..1748d77 --- /dev/null +++ b/aoc-2020-gleam/src/ext/resultx.gleam @@ -0,0 +1,4 @@ +pub fn force_unwrap(result: Result(t, _)) -> t { + assert Ok(value) = result + value +} diff --git a/aoc-2020-gleam/src/ext/stringx.gleam b/aoc-2020-gleam/src/ext/stringx.gleam new file mode 100644 index 0000000..6096465 --- /dev/null +++ b/aoc-2020-gleam/src/ext/stringx.gleam @@ -0,0 +1,5 @@ +import gleam/string + +pub fn is_not_empty(str: String) -> Bool { + !string.is_empty(str) +} 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) +} |