Skip to content

Commit 34c2c69

Browse files
2022 day 02 in Elixir
1 parent 98390fd commit 34c2c69

File tree

4 files changed

+2560
-27
lines changed

4 files changed

+2560
-27
lines changed

2022/elixir/lib/day02.ex

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
defmodule Day02 do
2+
@input File.read!("../input/day02.txt")
3+
@scores_part_one Enum.with_index([nil, :BX, :CY, :AZ, :AX, :BY, :CZ, :CX, :AY, :BZ])
4+
@scores_part_two Enum.with_index([nil, :BX, :CX, :AX, :AY, :BY, :CY, :CZ, :AZ, :BZ])
5+
6+
def part_one(input \\ @input), do: calculate_score(input, @scores_part_one)
7+
8+
def part_two(input \\ @input), do: calculate_score(input, @scores_part_two)
9+
10+
defp calculate_score(input, scores) do
11+
parse_input(input)
12+
|> Enum.map(&Keyword.get(scores, String.to_atom(&1)))
13+
|> Enum.sum()
14+
end
15+
16+
defp parse_input(input) do
17+
input
18+
|> String.split("\n", trim: true)
19+
|> Enum.map(&String.replace(&1, " ", ""))
20+
end
21+
end

2022/elixir/test/day02_test.exs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
defmodule Day02Test do
2+
use ExUnit.Case
3+
4+
@example_input """
5+
A Y
6+
B X
7+
C Z
8+
"""
9+
10+
test "part one", do: assert(Day02.part_one(@example_input) == 15)
11+
test "part two", do: assert(Day02.part_two(@example_input) == 12)
12+
end

0 commit comments

Comments
 (0)