From 918aaab8a65a5c4aff89333a3de417ac0c4369a8 Mon Sep 17 00:00:00 2001 From: Shanti Chellaram Date: Sat, 3 Dec 2022 13:11:36 +0900 Subject: [PATCH] Day 2-1 --- day2.erl | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 day2.erl diff --git a/day2.erl b/day2.erl new file mode 100644 index 0000000..86f0418 --- /dev/null +++ b/day2.erl @@ -0,0 +1,31 @@ +-module(day2). + +-export([solve/1]). + +solve(InputData) -> + RoundData = binary:split(InputData, <<$\n>>, [trim_all, global]), + Rounds = lists:map(fun parse_round/1, RoundData), + RoundPoints = lists:map(fun score_round/1, Rounds), + {lists:sum(RoundPoints), none}. + +parse_round(<>) -> {parse_theirs(TheirCode), parse_yours(YourCode)}. + +score_round(Round = {_Theirs, Yours}) -> round_points(Round) + shape_points(Yours). + +parse_theirs($A) -> rock; +parse_theirs($B) -> paper; +parse_theirs($C) -> scissors. + +parse_yours($X) -> rock; +parse_yours($Y) -> paper; +parse_yours($Z) -> scissors. + +round_points({rock, paper}) -> 6; +round_points({paper, scissors}) -> 6; +round_points({scissors, rock}) -> 6; +round_points({Shape, Shape}) -> 3; +round_points({_Theirs, _Yours}) -> 0. + +shape_points(rock) -> 1; +shape_points(paper) -> 2; +shape_points(scissors) -> 3.