Skip to content

Instantly share code, notes, and snippets.

@briankung
Last active December 4, 2022 04:45
Show Gist options
  • Save briankung/ad1405bc7b49daff73e8f40f84445ba2 to your computer and use it in GitHub Desktop.
Save briankung/ad1405bc7b49daff73e8f40f84445ba2 to your computer and use it in GitHub Desktop.
Advent of Code in Gleam
import gleam/int
import gleam/list
import gleam/erlang/file
import gleam/string
// 01.txt is a list of the caloric values of the snacks that elves are carrying.
// Each elf's personal inventory is separated from the other elves' inventories with
// two newlines "\n\n", and each snack is separated by a single newline "\n".
// In other words, the input string is a list of elf inventories, which is itself a
// list of snacks' caloric values.
fn parse_day_01() {
assert Ok(input) = file.read("./src/days/01.txt")
input
|> string.split(on: "\n\n")
|> list.map(fn(inventory) {
inventory
|> string.split(on: "\n")
|> list.filter_map(int.parse)
|> int.sum
})
}
pub fn part_one() {
parse_day_01()
|> list.fold(0, int.max)
}
pub fn part_two() {
parse_day_01()
|> list.sort(int.compare)
|> list.reverse
|> list.take(3)
|> int.sum
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment