Run this notebook

Use Livebook to open this notebook and explore new ideas.

It is easy to get started, on your machine or the cloud.

Click below to open and run it in your Livebook at .

(or change your Livebook location)

# 2022 Day 2 ```elixir Mix.install([:kino]) ``` ## Input [![Run in Livebook](https://livebook.dev/badge/v1/black.svg)](https://livebook.dev/run?url=https%3A%2F%2Fgithub.com%2FballPointPenguin%2Faoc2022%2Fblob%2Fmain%2Fday_02.livemd) ```elixir input = Kino.Input.textarea("Puzzle Input") ``` ## Code <h3> Module<h3> </h3> ```elixir defmodule RockPaperScissors do def total_score(input) do input |> process_input() |> Enum.map(&score/1) |> Enum.sum() end def strategic_score(input) do input |> process_input() |> Enum.map(&strategic/1) |> Enum.sum() end # Convert Letters to Integers # Rock: 1, Paper: 2, Scissors: 3 # or (Part 2) Loss: 1, Draw: 2, Win: 3 defp numerize(char) when char in ["A", "X"], do: 1 defp numerize(char) when char in ["B", "Y"], do: 2 defp numerize(char) when char in ["C", "Z"], do: 3 # draw defp score([num, num]), do: 3 + num # win defp score([3, 1]), do: 7 defp score([1, 2]), do: 8 defp score([2, 3]), do: 9 # loss defp score([_, num]), do: num # loss defp strategic([1, 1]), do: 3 defp strategic([them, 1]), do: them - 1 # draw defp strategic([them, 2]), do: them + 3 # win defp strategic([3, 3]), do: 7 defp strategic([them, 3]), do: them + 7 defp process_input(input) do input |> String.split() |> Enum.map(&numerize/1) |> Enum.chunk_every(2) end end ``` <h3> Evaluate<h3> </h3> ```elixir part_1 = input |> Kino.Input.read() |> RockPaperScissors.total_score() part_2 = input |> Kino.Input.read() |> RockPaperScissors.strategic_score() {part_1, part_2} ``` <h3> Test<h3> </h3> <!-- livebook:{"reevaluate_automatically":true} --> ```elixir ExUnit.start(autorun: false) defmodule RockPaperScissorsTest do use ExUnit.Case, async: true setup do input = ~s( A Y B X C Z ) {:ok, input: input} end test "total_score", %{input: input} do assert RockPaperScissors.total_score(input) == 15 end test "strategic_score", %{input: input} do assert RockPaperScissors.strategic_score(input) == 12 end end ExUnit.run() ```
See source

Have you already installed Livebook?

If you already installed Livebook, you can configure the default Livebook location where you want to open notebooks.
Livebook up Checking status We can't reach this Livebook (but we saved your preference anyway)
Run notebook

Not yet? Install Livebook in just a minute

Livebook is open source, free, and ready to run anywhere.

Run on your machine

with Livebook Desktop

Run in the cloud

on select platforms

To run on Linux, Docker, embedded devices, or Elixir’s Mix, check our README.

PLATINUM SPONSORS
SPONSORS
Code navigation with go to definition of modules and functions Read More ×