-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_03.ex
41 lines (36 loc) · 1.2 KB
/
day_03.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
defmodule Aoc.Y2022.Day03 do
@moduledoc """
Solved https://adventofcode.com/2022/day/3
"""
import Aoc.Helper.IO
def solve_part1(data) do
data
|> Enum.map(fn str -> String.split_at(str, div(String.length(str), 2)) end)
|> Enum.map(fn {str1, str2} ->
{str1 |> String.to_charlist() |> MapSet.new(), str2 |> String.to_charlist() |> MapSet.new()}
end)
|> Enum.map(fn {map1, map2} -> MapSet.intersection(map1, map2) |> MapSet.to_list() end)
|> Enum.map(&process/1)
|> Enum.sum()
end
def solve_part2(data) do
data
|> Enum.chunk_every(3)
|> Enum.map(fn [str1, str2, str3] ->
{str1 |> String.to_charlist() |> MapSet.new(), str2 |> String.to_charlist() |> MapSet.new(),
str3 |> String.to_charlist() |> MapSet.new()}
end)
|> Enum.map(fn {map1, map2, map3} ->
MapSet.intersection(map1, map2) |> MapSet.intersection(map3) |> MapSet.to_list()
end)
|> Enum.map(&process/1)
|> Enum.sum()
end
defp process([char]) when char > ?Z, do: rem(char, ?a) + 1
defp process([char]), do: rem(char, ?A) + 27
def get_input() do
get_string_input("2022", "03")
|> String.split("\n", trim: true)
end
def solved_status(), do: :solved
end