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)

# Cata 2 ```elixir Mix.install([{:kino, "<= 1.0.0"}]) ``` ## Part One ```elixir ``` Break down the problem: Part One: What is the problem I want to find the cheapest way to get from the top left to the bottom right number in an array. The costs are defined by the number on the index. I can only move one index right or down <!-- livebook:{"break_markdown":true} --> Part Two: Pseudocode ```elixir # data # check array item with index + 1 # check array item with row + 1 # compare and increase index or row # save number to array # array is result => Enum.sum ``` ```elixir defmodule Finder do use GenServer require Logger def get_next_fields() do GenServer.call(__MODULE__, :next_fields) end def start() do GenServer.start_link(__MODULE__, %{path: [], index: nil}) end def init(params) do Logger.debug("[#{__MODULE__}] got these parameters #{inspect(params)}") {:ok, params} end def raw() do " 1163751742 1381373672 2136511328 3694931569 7463417111 1319128137 1359912421 3125421639 1293138521 2311944581 " end def data() do Finder.raw() |> String.split("\n", trim: true) |> Enum.map(&String.trim/1) |> Enum.map(&String.split(&1, "", trim: true)) |> Enum.map(fn list -> Enum.map(list, &String.to_integer(&1)) end) |> Enum.reject(fn x -> x == [] end) |> Enum.with_index(1) |> Enum.map(fn {list, index} -> {Enum.with_index(list, 1), index} end) end def handle_call(:next_fields, _from, state) do fields = [4, 2] {:reply, fields, state} end def get_row({val, idx} = point) do Finder.data() |> Enum.map(fn {list, row} -> %{col: List.first(Enum.filter(list, fn {value, col} -> idx == col end)), row: row} end) end def get_next_row({val, idx} = point) do Finder.data() |> Enum.map(fn {list, row} -> %{col: List.first(Enum.filter(list, fn {value, col} -> idx + 1 == col end)), row: row} end) end end # |> Enum.map(fn list -> Enum.) ``` ```elixir Finder.start() Finder.get_next_fields() ```
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 ×