Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gleam solution day1 #65

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 61 additions & 1 deletion src/content/communitySolutions/01/dalurness.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down