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)

# Day 2 ```elixir Mix.install([ {:kino_aoc, git: "https://github.com/ljgago/kino_aoc"} ]) ``` ## Helper <!-- livebook:{"attrs":{"day":"2","session_secret":"AOC_SESSION","variable":"puzzle_input","year":"2022"},"kind":"Elixir.KinoAOC.HelperCell","livebook_object":"smart_cell"} --> ```elixir {:ok, puzzle_input} = KinoAOC.download_puzzle("2022", "2", System.fetch_env!("LB_AOC_SESSION")) ``` A -> Rock B -> Paper C -> Scissor --- X -> Rock Y -> Paper Z -> Scissor --- ### Score 1 -> Rock 2 -> Paper 3 -> Scissor Plus 0(lost) | 3(draw) | 6(win) ```elixir puzzle_input = puzzle_input |> String.split("\n", trim: true) |> Enum.map(&String.split/1) ``` ## Part 1 ```elixir defmodule GameLogic do def score(enemy_move, player_move) do case move_decision(enemy_move, player_move) do {:win, point} -> point + 6 {:loss, point} -> point + 0 {:draw, point} -> point + 3 end end def move_decision("A", player_move) do case player_move do "X" -> {:draw, 1} "Y" -> {:win, 2} "Z" -> {:loss, 3} end end def move_decision("B", player_move) do case player_move do "X" -> {:loss, 1} "Y" -> {:draw, 2} "Z" -> {:win, 3} end end def move_decision("C", player_move) do case player_move do "X" -> {:win, 1} "Y" -> {:loss, 2} "Z" -> {:draw, 3} end end end ``` ```elixir Enum.reduce(puzzle_input, 0, fn [enemy_move, player_move], total_score -> total_score + GameLogic.score(enemy_move, player_move) end) ``` ## Part 2 ```elixir defmodule GameLogicPart2 do def score(enemy_move, player_move) do case move_decision(enemy_move, player_move) do {:win, point} -> point + 6 {:loss, point} -> point + 0 {:draw, point} -> point + 3 end end # Rock def move_decision("A", player_move) do case player_move do "X" -> {:loss, 3} "Y" -> {:draw, 1} "Z" -> {:win, 2} end end # Paper def move_decision("B", player_move) do case player_move do "X" -> {:loss, 1} "Y" -> {:draw, 2} "Z" -> {:win, 3} end end # Scissor def move_decision("C", player_move) do case player_move do "X" -> {:loss, 2} "Y" -> {:draw, 3} "Z" -> {:win, 1} end end end ``` ```elixir Enum.reduce(puzzle_input, 0, fn [enemy_move, player_move], total_score -> total_score + GameLogicPart2.score(enemy_move, player_move) end) ```
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 ×