DEV Community

Caleb Weeks
Caleb Weeks

Posted on • Edited on • Originally published at sethcalebweeks.com

4 2

Advent of Code Day 3

Links

Highlights

  • 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
    input(3)
    ~> String.split("\n")
    ~> Enum.map(fn sack ->
      middle = div(String.length(sack), 2)
      {first, second} = String.split_at(sack, middle)
      first
      ~> String.to_charlist()
      ~> Enum.reduce(false, fn
        _, acc when is_integer(acc) -> acc
        char, _ -> String.contains?(second, <<char>>) && value(char)
      end)
    end)
    ~> Enum.sum
  end

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

end
Enter fullscreen mode Exit fullscreen mode

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay