🎄 Christmas Coding with Elixir, Day 2

Advent Of Code 2018

Santa: Big Elixir fan

The Puzzle: Part 1

Solving It

Here’s our input:

> ["a", "a", "a", "b", "b", "c"] |> Enum.group_by(&(&1))
%{"a" => ["a", "a", "a"], "b" => ["b", "b"], "c" => ["c"]}
> ["aaabbc", "def"] |> Boxes.group_by_letters
[[{"a", 3}, {"b", 2}, {"c", 1}], [{"d", 1}, {"e", 1}, {"f", 1}]]
$ cat input.txt | ./advent2.1.exs
9633

The Puzzle: Part 2

Solving It

Here things get a little trickier.

> [["a", "a", "a"], ["b", "b", "b"]] |> Enum.zip()
[{"a", "b"}, {"a", "b"}, {"a", "b"}]
> for a <- 1..3, b <- 1..3, do: [a, b]
[[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], [3, 1], [3, 2], [3, 3]]
$ cat input.txt | ./advent2.2.exs
lujnogabetpmsydyfcovzixaw

Summary

The list comprehensions in Elixir are very powerful for generating data. I strongly recommend checking out the documentation and then experimenting with them.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sean Handley

Señor Developer specialising in open-source languages, particularly Ruby & Elixir. seanhandley.com