Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Day 8

day8.livemd

Day 8

Mix.install([{:kino, "~> 0.5.0"}])

Section

input = """
30373
25512
65332
33549
35390
"""
input = Kino.Input.textarea("paste input here:")
input = Kino.Input.read(input)

Part 1

grid = input |> String.split("\n", trim: true) |> Enum.map(&String.to_charlist/1)
transposed = Enum.zip(grid) |> Enum.map(&Tuple.to_list/1)

for row <- 0..(length(grid) - 1), col <- 0..(length(hd(grid)) - 1) do
  {left, [_current | right]} = Enum.split(Enum.at(grid, row), col)
  {top, [current | bottom]} = Enum.split(Enum.at(transposed, col), row)

  Enum.max([0 | left]) < current || Enum.max([0 | right]) < current ||
    Enum.max([0 | top]) < current || Enum.max([0 | bottom]) < current
end
|> Enum.count(&amp; &amp;1)

Part 2

defmodule Part2 do
  def run(input) do
    grid = input |> String.split("\n", trim: true) |> Enum.map(&amp;String.to_charlist/1)
    transposed = Enum.zip(grid) |> Enum.map(&amp;Tuple.to_list/1)

    for row <- 1..(length(grid) - 2), col <- 1..(length(hd(grid)) - 2) do
      {left, [_current | right]} = Enum.split(Enum.at(grid, row), col)
      {top, [current | bottom]} = Enum.split(Enum.at(transposed, col), row)

      [Enum.reverse(left), right, Enum.reverse(top), bottom]
      |> Enum.map(fn l -> Enum.split_while(l, &amp;(&amp;1 < current)) end)
      |> Enum.map(fn {h, t} -> Enum.count(h) + if Enum.empty?(t), do: 0, else: 1 end)
      |> Enum.product()
    end
    |> Enum.max()
  end
end
Part2.run(input)