r/adventofcode • • Dec 02 '22

SOLUTION MEGATHREAD -🎄- 2022 Day 2 Solutions -🎄-

NEW AND NOTEWORTHY


--- Day 2: Rock Paper Scissors ---


Post your code solution in this megathread.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:06:16, megathread unlocked!

103 Upvotes

1.5k comments sorted by

View all comments

2

u/seacucumber_kid Dec 02 '22 edited Dec 02 '22

Elixir

Wanted to have it all in a single pipeline lol

values = %{"A" => 0, "B" => 1, "C" => 2, "X" => 1, "Y" => 2, "Z" => 3}

File.read!("input.txt")
|> String.split("\n")
|> tap(
  &(Enum.reduce(
      &1,
      0,
      fn <<a::binary-size(1), " ", b::binary>> = ab, acc ->
        acc + values[b] +
          cond do
            ab == "A Y" -> 6
            ab == "B Z" -> 6
            ab == "C X" -> 6
            values[a] + 1 == values[b] -> 3
            true -> 0
          end
      end
    )
    |> IO.inspect(label: "Part 1"))
)
|> tap(
  &(Enum.reduce(
      &1,
      0,
      fn <<a::binary-size(1), " ", b::binary>>, acc ->
        acc +
          case b do
            "X" -> Integer.mod(values[a] - 1, 3) + 1
            "Y" -> values[a] + 1 + 3
            "Z" -> Integer.mod(values[a] + 1, 3) + 1 + 6
          end
      end
    )
    |> IO.inspect(label: "Part 2"))
)

1

u/daggerdragon Dec 05 '22

Please edit your post to use the four-spaces Markdown syntax for a code block so your code is easier to read on old.reddit and mobile apps.

1

u/kawzeg Dec 02 '22

I think you could replace _::binary-size(1) with " " and b::binary-size(1) with just band the pattern matching should still work.

1

u/seacucumber_kid Dec 02 '22

Indeed, but b needs to be b::binary. Thanks :)

1

u/kawzeg Dec 02 '22

Ah right! Now I remember why I had inserted an Enum.map(&String.split/1) and matched on [a, b] instead :D