aoc/22/lib/days/day2.ex
2023-12-04 10:25:33 +01:00

68 lines
1.2 KiB
Elixir

defmodule AOC.Day2 do
use AOC.Day, day: 2
def parse_input(lines) do
Enum.map(lines, fn line ->
[move1, move2] =
line
|> String.split(" ")
|> Enum.map(fn c ->
c
|> String.to_charlist()
|> hd()
end)
{move1, move2}
end)
end
def part1(input) do
input
|> Enum.map(fn {move1, move2} ->
score =
case move2 do
?X -> 1
?Y -> 2
?Z -> 3
end
score +
case {move1, move2} do
{?A, ?Y} -> 6
{?B, ?Z} -> 6
{?C, ?X} -> 6
{?A, ?X} -> 3
{?B, ?Y} -> 3
{?C, ?Z} -> 3
_ -> 0
end
end)
|> Enum.sum()
end
def part2(input) do
input
|> Enum.map(fn {move1, result} ->
score =
case result do
?X -> 0
?Y -> 3
?Z -> 6
end
score +
case {move1, result} do
{?A, ?X} -> 3
{?A, ?Y} -> 1
{?A, ?Z} -> 2
{?B, ?X} -> 1
{?B, ?Y} -> 2
{?B, ?Z} -> 3
{?C, ?X} -> 2
{?C, ?Y} -> 3
{?C, ?Z} -> 1
end
end)
|> Enum.sum()
end
end