DEV Community

Caleb Weeks
Caleb Weeks

Posted on • Updated on • Originally published at


Advent of Code Day 3



  • As in most programming languages, strings are represented as binary code points in Elixir. The ? operator can be used to match on a range of characters, and the <<>> can be used to get the code of a string.
  • For the first part, I split each line in half and checked to see if each item in the first half was in the second half using a reduce. As soon as a match is found, the value is calculated and replaces the accumulator. Any subsequent calls to reduce simply return the accumulator as is if it is already an integer.
  • For the second part, I chunked the lines by three. For each group of three, I got rid of duplicates within each sack and flattened all three into a single list. Then I calculated the frequencies of each character and found the character with a frequency of three.
defmodule Day03 do
  use AOC

  def value(code) when code in ?a..?z, do: code - 96
  def value(code) when code in ?A..?Z, do: code - 38

  def part1 do
    ~> String.split("\n")
    ~> sack ->
      middle = div(String.length(sack), 2)
      {first, second} = String.split_at(sack, middle)
      ~> String.to_charlist()
      ~> Enum.reduce(false, fn
        _, acc when is_integer(acc) -> acc
        char, _ -> String.contains?(second, <<char>>) && value(char)
    ~> Enum.sum

  def part2 do
    ~> String.split("\n")
    ~> Enum.chunk_every(3)
    ~> chunk ->
        ~> Enum.flat_map(&Enum.uniq(String.to_charlist(&1)))
        ~> Enum.frequencies()
        ~> Enum.find(fn {_, freq} -> freq == 3 end)
        ~> fn {char, _} -> value(char) end
    ~> Enum.sum

Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Join us at DEV Find what you were looking for? Sign up so you can:
🌚 Enable dark mode
🔠 Change your default font
📚 Adjust your experience level to see more relevant content