Skip to content

Instantly share code, notes, and snippets.

@bodgix
Created December 5, 2021 00:32
Show Gist options
  • Save bodgix/8ab16a0f6cb4a40185fd3d1514cc638c to your computer and use it in GitHub Desktop.
Save bodgix/8ab16a0f6cb4a40185fd3d1514cc638c to your computer and use it in GitHub Desktop.
defmodule Board do
defstruct numbers: %{}, hits: %{}, last_match: nil
def parse_xy_val(lines) when is_list(lines) do
board =
lines
|> Enum.with_index()
|> Enum.reduce(%{}, &parse_line_xy_val/2)
%__MODULE__{numbers: board}
end
def parse_val_xy(lines) when is_list(lines) do
board =
lines
|> Enum.with_index()
|> Enum.reduce(%{}, &parse_line_val_xy/2)
%__MODULE__{numbers: board}
end
defp parse_line_xy_val({line, row_num}, board) do
line
|> String.split()
|> Enum.map(&String.to_integer/1)
|> Enum.with_index()
|> Enum.reduce(board, fn {val, col}, acc ->
Map.put(acc, {row_num, col}, val)
end)
end
defp parse_line_val_xy({line, row_num}, board) do
line
|> String.split()
|> Enum.map(&String.to_integer/1)
|> Enum.with_index()
|> Enum.reduce(board, fn {val, col}, acc ->
Map.put(acc, val, {row_num, col})
end)
end
end
"""
22 13 17 11 0
8 2 23 4 24
21 9 14 16 7
6 10 3 18 5
1 12 20 15 19
"""
|> String.split("\n")
|> Enum.map(&String.trim/1)
|> Board.parse_xy_val()
|> IO.inspect(label: "Board {x, y} -> val")
"""
22 13 17 11 0
8 2 23 4 24
21 9 14 16 7
6 10 3 18 5
1 12 20 15 19
"""
|> String.split("\n")
|> Enum.map(&String.trim/1)
|> Board.parse_val_xy()
|> IO.inspect(label: "Board val -> {x, y}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment