Advent of Code 2022 Day 2 Discussions

This topic is about Day 2 of the Advent of Code 2022 .

I am leaving this post open so that folks can discuss their day 2 adventures when it begins (12:00 AM EST)!

Link to our leaderboard:

https://adventofcode.com/2022/leaderboard/private/view/370884

The entry code is:
370884-a6a71927

Good luck!

3 Likes

Made super dumb mistake in rock-paper-scissors logic on the edge case and almost convinced myself that there was an error in the input since my answer was not going through T_T, anyway here’s my solution:

main(File) ->
    {ok, RawData} = file:read_file(File),
    Data = [ begin
                  [A, B] = binary:split(N, <<" ">>, [global, trim]),
                  {b2a(A), b2a(B), b2s(B)}
             end || N <- binary:split(RawData, <<"\n">>, [global, trim]) ],
    io:format("part 1: ~p~n", [solve1(Data)]),
    io:format("part 2: ~p~n", [solve2(Data)]).

solve1(Data) ->
    lists:sum([ rock_paper_scissors(A, B) || {A, B, _} <- Data ]).

solve2(Data) ->
    lists:sum([ rock_paper_scissors(A, strategy(A, C)) || {A, _, C} <- Data ]).

b2a(<<"A">>) -> 1;
b2a(<<"B">>) -> 2;
b2a(<<"C">>) -> 3;
b2a(<<"X">>) -> 1;
b2a(<<"Y">>) -> 2;
b2a(<<"Z">>) -> 3.

rock_paper_scissors(A, B) when A =:= B                       -> 3 + B;
rock_paper_scissors(A, B) when A =:= 3, B =:= 1; B - A =:= 1 -> 6 + B;
rock_paper_scissors(_, B)                                    -> 0 + B.

b2s(<<"X">>) -> lose;
b2s(<<"Y">>) -> draw;
b2s(<<"Z">>) -> win.

strategy(A, lose) -> lists:nth(A, [3, 1, 2]);
strategy(A, draw) -> A;
strategy(A, win)  -> lists:nth(A, [2, 3, 1]).
3 Likes

Here’s my code for Day 2.

-module(day_2).

-export([solve/0]).

input() ->
  {ok, Turns} = file:read_file("data/2.txt"),
  Turns.

solve() ->
  Turns = parse(input()),
  Possibilities =
    #{<<"A X">> => {4, 3},
      <<"A Y">> => {8, 4},
      <<"A Z">> => {3, 8},
      <<"B X">> => {1, 1},
      <<"B Y">> => {5, 5},
      <<"B Z">> => {9, 9},
      <<"C X">> => {7, 2},
      <<"C Y">> => {2, 6},
      <<"C Z">> => {6, 7}},
  Solve1 = solve1(Turns, Possibilities),
  Solve2 = solve2(Turns, Possibilities),
  {Solve1, Solve2}.

solve1(Turns, Possibilities) ->
  lists:sum([Score
             || {ok, {Score, _}} <- [maps:find(Turn, Possibilities) || Turn <- Turns]]).

solve2(Turns, Possibilities) ->
  lists:sum([Score
             || {ok, {_, Score}} <- [maps:find(Turn, Possibilities) || Turn <- Turns]]).

parse(Turns) ->
  [X || X <- binary:split(Turns, <<"\n">>, [global, trim])].

2 Likes