diff --git a/src/content/communitySolutions/01/dalurness.md b/src/content/communitySolutions/01/dalurness.md index c82132e..86bdd81 100644 --- a/src/content/communitySolutions/01/dalurness.md +++ b/src/content/communitySolutions/01/dalurness.md @@ -1,7 +1,67 @@ --- -descriptions: ["elixir", "first time using language"] +descriptions: ["gleam", "elixir", "first time using language"] --- +### 2024 Solution Gleam + +```gleam +import gleam/dict +import gleam/int +import gleam/io +import gleam/list +import gleam/string +import simplifile + +pub fn main() { + ["letters.txt", "letters_challenge.txt"] + |> list.each(find_country_count) +} + +fn find_country_count(filename: String) { + let solution_filename = "solution_" <> filename + case simplifile.create_file(solution_filename) { + Ok(_) -> io.println("created file") + Error(e) -> { + io.debug(e) + Nil + } + } + + case simplifile.read(filename) { + Ok(file_contents) -> file_contents + Error(_) -> { + io.println("failed to read file: " <> filename) + panic as "failed to read file" + } + } + |> string.trim() + |> string.split(" ") + |> list.fold(dict.new(), fn(acc, country_code) { + case dict.has_key(acc, country_code) { + True -> { + case dict.get(acc, country_code) { + Error(_) -> + panic as "how could it not be in there? We just checked..." + Ok(value) -> { + dict.insert(acc, country_code, value + 1) + } + } + } + False -> dict.insert(acc, country_code, 1) + } + }) + // collect and output result + |> dict.each(fn(cc, amount) { + simplifile.append( + solution_filename, + cc <> ": " <> int.to_string(amount) <> "\n", + ) + }) +} +``` + +### 2023 Solution Elixir + ```elixir writeString = case File.read("../letters.txt") do {:ok, body} -> body