Skip to content

Commit

Permalink
Merge pull request #65 from dalurness/du/day1Solution
Browse files Browse the repository at this point in the history
gleam solution day1
  • Loading branch information
dalurness authored Dec 2, 2024
2 parents 191d2d5 + 97fe7c3 commit c779fc6
Showing 1 changed file with 61 additions and 1 deletion.
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

0 comments on commit c779fc6

Please sign in to comment.