aoc/22/lib/days/day3.ex

40 lines
889 B
Elixir
Raw Normal View History

2023-04-17 18:18:20 +00:00
defmodule AOC.Day3 do
use AOC.Day, day: 3
def parse_input(lines), do: lines
def part1(input) do
input
|> Enum.map(fn line ->
comp_size = line |> String.length() |> div(2)
{comp1, comp2} = line |> String.to_charlist() |> Enum.split(comp_size)
comp1 = MapSet.new(comp1)
comp2 = MapSet.new(comp2)
MapSet.intersection(comp1, comp2)
|> MapSet.to_list()
|> hd()
|> get_priority()
end)
|> Enum.sum()
end
def part2(input) do
input
|> Enum.chunk_every(3)
|> Enum.map(fn group ->
group
|> Enum.map(&String.to_charlist/1)
|> Enum.map(&MapSet.new/1)
|> Enum.reduce(&MapSet.intersection/2)
|> MapSet.to_list()
|> hd()
|> get_priority()
end)
|> Enum.sum()
end
def get_priority(char) when char >= ?a, do: char - 96
def get_priority(char), do: char - 38
end