This content originally appeared on DEV Community 👩💻👨💻 and was authored by Caleb Weeks
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
This content originally appeared on DEV Community 👩💻👨💻 and was authored by Caleb Weeks
Print
Share
Comment
Cite
Upload
Translate
Updates
There are no updates yet.
Click the Upload button above to add an update.
APA
MLA
Caleb Weeks | Sciencx (2022-12-04T15:19:08+00:00) Advent of Code Day 4. Retrieved from https://www.scien.cx/2022/12/04/advent-of-code-day-4/
" » Advent of Code Day 4." Caleb Weeks | Sciencx - Sunday December 4, 2022, https://www.scien.cx/2022/12/04/advent-of-code-day-4/
HARVARDCaleb Weeks | Sciencx Sunday December 4, 2022 » Advent of Code Day 4., viewed ,<https://www.scien.cx/2022/12/04/advent-of-code-day-4/>
VANCOUVERCaleb Weeks | Sciencx - » Advent of Code Day 4. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/12/04/advent-of-code-day-4/
CHICAGO" » Advent of Code Day 4." Caleb Weeks | Sciencx - Accessed . https://www.scien.cx/2022/12/04/advent-of-code-day-4/
IEEE" » Advent of Code Day 4." Caleb Weeks | Sciencx [Online]. Available: https://www.scien.cx/2022/12/04/advent-of-code-day-4/. [Accessed: ]
rf:citation » Advent of Code Day 4 | Caleb Weeks | Sciencx | https://www.scien.cx/2022/12/04/advent-of-code-day-4/ |
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.