Advent Of Code – Day 02
Data
stream = File.stream!("/Users/pw/src/weiland/adventofcode/2022/input/02.txt")
stream
convert_line = fn line ->
line
|> String.replace("\n", "")
|> String.split(" ")
end
stream = Stream.map(stream, convert_line)
Code
Part one:
calculate_score = fn line ->
[op, me] = line
case {op, me} do
{"A", "X"} -> 4
{"A", "Y"} -> 8
{"A", "Z"} -> 3
{"B", "X"} -> 1
{"B", "Y"} -> 5
{"B", "Z"} -> 9
{"C", "X"} -> 7
{"C", "Y"} -> 2
{"C", "Z"} -> 6
_ -> 999_999
end
end
stream
|> Enum.map(calculate_score)
|> Enum.sum()
Part two:
calculate_score_two = fn line ->
[op, me] = line
case {op, me} do
{"A", "X"} -> 3
{"A", "Y"} -> 4
{"A", "Z"} -> 8
{"B", "X"} -> 1
{"B", "Y"} -> 5
{"B", "Z"} -> 9
{"C", "X"} -> 2
{"C", "Y"} -> 6
{"C", "Z"} -> 7
_ -> 999_999
end
end
stream
|> Enum.map(calculate_score_two)
|> Enum.sum()