aoc/22/lib/days/day4.ex

39 lines
753 B
Elixir
Raw Normal View History

2023-04-17 18:18:20 +00:00
defmodule AOC.Day4 do
use AOC.Day, day: 4
def parse_input(lines) do
Enum.map(lines, fn line ->
[assign1, assign2] =
line
|> String.split(",")
|> Enum.map(fn assign ->
[from, to] =
assign
|> String.split("-")
|> Enum.map(&String.to_integer/1)
MapSet.new(from..to)
end)
{assign1, assign2}
end)
end
def part1(input) do
input
|> Enum.map(fn {set1, set2} ->
MapSet.subset?(set1, set2) or MapSet.subset?(set2, set1)
end)
|> Enum.count(& &1)
end
def part2(input) do
input
|> Enum.map(fn {set1, set2} ->
MapSet.intersection(set1, set2)
|> Enum.any?()
end)
|> Enum.count(& &1)
end
end