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

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

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

Okay