aoc/2022/lib/days/day8.ex

54 lines
1.1 KiB
Elixir
Raw Normal View History

2023-04-17 18:18:20 +00:00
defmodule AOC.Day8 do
use AOC.Day, day: 8, debug: true
def parse_input(lines) do
Enum.map(lines, fn line ->
line
|> String.split("", trim: true)
|> Enum.map(&String.to_integer/1)
end)
end
def part1(rows) do
rows = with_index(rows)
cols = Enum.zip(rows) |> Enum.map(&Tuple.to_list/1)
rrows = Enum.map(rows, &Enum.reverse/1)
rcols = Enum.map(cols, &Enum.reverse/1)
[rows, cols, rrows, rcols]
|> Enum.map(&count_visible/1)
|> Enum.reduce(&MapSet.union/2)
|> Enum.count()
end
def part2(rows) do
rows
end
def with_index(rows) do
Enum.with_index(rows, fn row, y ->
Enum.with_index(row, fn el, x ->
%{height: el, x: x, y: y}
end)
end)
end
def count_visible(rows) do
rows
|> Enum.map(&count_visible_row/1)
|> Enum.map(&elem(&1, 0))
|> Enum.reduce(&MapSet.union/2)
end
def count_visible_row(row) do
Enum.reduce(row, {MapSet.new(), -1}, fn
%{height: h, x: x, y: y}, {trees, highest} when h > highest ->
{MapSet.put(trees, {x, y}), h}
_, acc ->
acc
end)
end
end