Skip to content

Instantly share code, notes, and snippets.

@ynonp
Created December 6, 2017 04:23
Show Gist options
  • Save ynonp/7fd9279c7cbdd1052656a846ba3f6d09 to your computer and use it in GitHub Desktop.
Save ynonp/7fd9279c7cbdd1052656a846ba3f6d09 to your computer and use it in GitHub Desktop.
defmodule Day5 do
def list_to_indexed_map(list) do
list
|> Enum.with_index(0)
|> Enum.map(fn({k,v}) -> {v,k} end)
|> Map.new
end
def next({ arr, ip }) do
try do
jump = Map.fetch!(arr, ip)
diff = if jump >= 3, do: -1, else: 1
{
arr,
{
Map.replace(arr, ip, jump + diff),
ip + jump,
}
}
rescue
KeyError -> nil
end
end
def parse(line) do
Stream.unfold({ line, 0 }, &Day5.next/1)
end
end
IO.stream(:stdio, :line)
|> Stream.map(&String.trim/1)
|> Stream.map(&String.to_integer/1)
|> Enum.to_list
|> Day5.list_to_indexed_map
|> Day5.parse
|> Enum.count
|> IO.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment