DEV Community

Caleb Weeks
Caleb Weeks

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

2

Advent of Code Day 4

Links

Highlights

  • Nothing too extraordinary with today's problem. For the second part, I started by only checking to see if either endpoint of one elf was within the endpoints of the other elf. Honestly, I still can't figure out why this didn't work, and I had to do the same check in the other direction. Maybe it will come to me at some point today.
defmodule Day04 do
  use AOC

  def part1 do
    input(4)
    ~> String.split("\n")
    ~> Enum.map(fn pair ->
      ends = Regex.named_captures(~r/(?<l1>.*)-(?<h1>.*),(?<l2>.*)-(?<h2>.*)/, pair)
      ~> Map.new(fn {key, value} -> {String.to_atom(key), String.to_integer(value)} end)
      (ends.l1 <= ends.l2 && ends.h1 >= ends.h2) ||
      (ends.l2 <= ends.l1 && ends.h2 >= ends.h1)
    end)
    ~> Enum.count(&Function.identity(&1))
  end

  def part2 do
    input(4)
    ~> String.split("\n")
    ~> Enum.map(fn pair ->
      ends = Regex.named_captures(~r/(?<l1>.*)-(?<h1>.*),(?<l2>.*)-(?<h2>.*)/, pair)
      ~> Map.new(fn {key, value} -> {String.to_atom(key), String.to_integer(value)} end)
      (ends.l1 >= ends.l2 && ends.l1 <= ends.h2) ||
      (ends.h1 >= ends.l2 && ends.h1 <= ends.h2) ||
      (ends.l2 >= ends.l1 && ends.l2 <= ends.h1) ||
      (ends.h2 >= ends.l1 && ends.h2 <= ends.h1)
    end)
    ~> Enum.count(&Function.identity(&1))
  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 full post →

Top comments (0)

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

👋 Kindness is contagious

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

Okay