christhekeele

christhekeele

Advent of Code 2022 - Day 2

Continuation of Advent of Code 2022​:christmas_tree:, Day 1:

Day 2!

Leaderboard:

First Post! Switch mode

christhekeele

christhekeele OP

My solution lives here, with a test suite.


Part 1:
defmodule AoC.TwentyTwentyTwo.Day.Two.Part.One do
  def solve(input) do
    input
    |> Enum.map(&score_round/1)
    |> Enum.sum()
  end

  defp score_round({"A", "X"}), do: 1 + 3
  defp score_round({"A", "Y"}), do: 2 + 6
  defp score_round({"A", "Z"}), do: 3 + 0

  defp score_round({"B", "X"}), do: 1 + 0
  defp score_round({"B", "Y"}), do: 2 + 3
  defp score_round({"B", "Z"}), do: 3 + 6

  defp score_round({"C", "X"}), do: 1 + 6
  defp score_round({"C", "Y"}), do: 2 + 0
  defp score_round({"C", "Z"}), do: 3 + 3
end

Part 2:
defmodule AoC.TwentyTwentyTwo.Day.Two.Part.Two do
  def solve(input) do
    input
    |> Enum.map(&score_round/1)
    |> Enum.sum()
  end

  defp score_round({"A", "X"}), do: 0 + 3
  defp score_round({"A", "Y"}), do: 3 + 1
  defp score_round({"A", "Z"}), do: 6 + 2

  defp score_round({"B", "X"}), do: 0 + 1
  defp score_round({"B", "Y"}), do: 3 + 2
  defp score_round({"B", "Z"}), do: 6 + 3

  defp score_round({"C", "X"}), do: 0 + 2
  defp score_round({"C", "Y"}), do: 3 + 3
  defp score_round({"C", "Z"}), do: 6 + 1
end

A little hard-coded-y; I could have separated the scoring between win or lose, and the two different ways to interpret the second letter in each instruction.

Most Liked

kwando

kwando

One of my colleagues came up with something completely different which I thought was interesting:

(This is translated from python to elixir)

 File.stream!("/Users/kwando/projects/AoC2022/02/input.txt")
 |> Stream.map(fn line -> String.trim(line) |> String.to_charlist() end)
 |> Enum.reduce(0, fn [elf, _, you], score ->
  shape_score = you - ?X + 1

  case {elf - ?A, you - ?X} do
    {any, any} ->
      score + 3 + shape_score
    {elf, you} when rem(you + 2, 3) == elf ->
      score + 6 + shape_score
    {_, _} ->
      score + shape_score
  end
end)

https://github.com/nilskj/AdventOfCode2022/blob/master/day02a.py

LostKobrakai

LostKobrakai

I did write a bit more code, but I used metaprogramming to generate all the score cases instead of hardcoding them. Would’ve been more useful it this hadn’t been just a 3x3 grid of possible inputs per line.

Solution
defmodule Day2 do
  def find_score(text) do
    text
    |> String.split("\n")
    |> Enum.reject(&(&1 == ""))
    |> Enum.map(&score/1)
    |> Enum.sum()
  end

  def find_score_alternate(text) do
    text
    |> String.split("\n")
    |> Enum.reject(&(&1 == ""))
    |> Enum.map(&score_alternate/1)
    |> Enum.sum()
  end

  @score_per_type %{rock: 1, paper: 2, scissors: 3}
  @score_per_result %{loss: 0, draw: 3, win: 6}

  scores_per_round =
    for opponent <- Map.keys(@score_per_type),
        myself <- Map.keys(@score_per_type),
        into: %{} do
      result =
        case {opponent, myself} do
          {x, x} -> :draw
          {:scissors, :rock} -> :win
          {:paper, :scissors} -> :win
          {:rock, :paper} -> :win
          _ -> :loss
        end

      opponent_key =
        case opponent do
          :rock -> "A"
          :paper -> "B"
          :scissors -> "C"
        end

      myself_key =
        case myself do
          :rock -> "X"
          :paper -> "Y"
          :scissors -> "Z"
        end

      {"#{opponent_key} #{myself_key}",
       Map.fetch!(@score_per_result, result) + Map.fetch!(@score_per_type, myself)}
    end

  for {round, score} <- scores_per_round do
    defp score(unquote(round)), do: unquote(score)
  end

  scores_per_round_alternate =
    for opponent <- Map.keys(@score_per_type),
        result <- Map.keys(@score_per_result),
        into: %{} do
      myself =
        case {opponent, result} do
          {x, :draw} -> x
          {:scissors, :win} -> :rock
          {:paper, :win} -> :scissors
          {:rock, :win} -> :paper
          {:scissors, :loss} -> :paper
          {:paper, :loss} -> :rock
          {:rock, :loss} -> :scissors
        end

      opponent_key =
        case opponent do
          :rock -> "A"
          :paper -> "B"
          :scissors -> "C"
        end

      result_key =
        case result do
          :loss -> "X"
          :draw -> "Y"
          :win -> "Z"
        end

      {"#{opponent_key} #{result_key}",
       Map.fetch!(@score_per_result, result) + Map.fetch!(@score_per_type, myself)}
    end

  for {round, score} <- scores_per_round_alternate do
    defp score_alternate(unquote(round)), do: unquote(score)
  end

  @doc false
  def debug, do: unquote(Macro.escape(scores_per_round))

  @doc false
  def debug_alternate, do: unquote(Macro.escape(scores_per_round_alternate))
end
adamu

adamu

I went for “be very explicit” on this one :grinning_face_with_smiling_eyes:

  defp play({:rock, :rock}), do: @rock + @draw
  defp play({:paper, :rock}), do: @rock + @lose
  defp play({:scissors, :rock}), do: @rock + @win
  defp play({:rock, :paper}), do: @paper + @win
  defp play({:paper, :paper}), do: @paper + @draw
  defp play({:scissors, :paper}), do: @paper + @lose
  defp play({:rock, :scissors}), do: @scissors + @lose
  defp play({:paper, :scissors}), do: @scissors + @win
  defp play({:scissors, :scissors}), do: @scissors + @draw

https://git.adamu.jp/adam/AdventOfCode/src/branch/main/2022/day2.exs

Last Post!

dmarcoux

dmarcoux

I’m pretty new to Elixir, so feedback is always appreciated!

https://github.com/dmarcoux/advent_of_code_2022/blob/main/lib/advent_of_code2022/day2.ex

Where Next?

Trending in Challenges Top

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement