LostKobrakai
This one has been quite the ride. Struggled at first to find a good data format to suite the problem. I really like how that turned out by separating the map data from coordinates to look at when counting. Part 2 also was imo not well defined. It took me a while to figure out I don’t need to subtract smaller trees behind larger trees anymore.
Solution
defmodule Day8 do
defstruct map: nil, size: nil
def parse(text) do
lines = text |> String.split("\n") |> Enum.reject(&(&1 == ""))
{map, _} =
lines
|> Enum.with_index()
|> Enum.flat_map_reduce(0, fn {line, y}, next ->
line
|> String.split("", trim: true)
|> Enum.with_index()
|> Enum.map_reduce(next, fn {height, x}, next ->
item = {{x, y}, %{id: next, height: String.to_integer(height)}}
{item, next + 1}
end)
end)
map = Map.new(map)
keys = Map.keys(map)
size_x = keys |> Enum.map(fn {x, _} -> x end) |> Enum.max()
size_y = keys |> Enum.map(fn {_, y} -> y end) |> Enum.max()
%__MODULE__{map: map, size: %{x: size_x, y: size_y}}
end
def count_visible_from_outside(text) do
data = parse(text)
from_left_keys =
for y <- 0..data.size.y//1 do
for x <- 0..data.size.x//1, do: {x, y}
end
from_right_keys =
for y <- 0..data.size.y//1 do
for x <- data.size.x..0//-1, do: {x, y}
end
from_top_keys =
for x <- 0..data.size.x//1 do
for y <- 0..data.size.y//1, do: {x, y}
end
from_bottom_keys =
for x <- 0..data.size.x//1 do
for y <- data.size.y..0//-1, do: {x, y}
end
[
from_left_keys,
from_right_keys,
from_top_keys,
from_bottom_keys
]
|> Enum.flat_map(& &1)
|> Enum.flat_map(&count_visible_line(data.map, &1))
|> Enum.uniq()
|> Enum.count()
end
defp count_visible_line(map, line) do
{_, trees} =
line
|> Enum.map(fn coordinate -> Map.fetch!(map, coordinate) end)
|> Enum.reduce({-1, []}, fn
%{height: tree_height} = tree, {line_of_sight, visible}
when tree_height > line_of_sight ->
{tree_height, [tree | visible]}
_, acc ->
acc
end)
trees
end
def count_visible_from_tree_house(text) do
data = parse(text)
for y <- 0..data.size.y//1, x <- 0..data.size.x//1 do
tree = Map.fetch!(data.map, {x, y})
to_left = for x <- (x - 1)..0//-1, do: {x, y}
to_right = for x <- (x + 1)..data.size.x//1, do: {x, y}
to_top = for y <- (y - 1)..0//-1, do: {x, y}
to_bottom = for y <- (y + 1)..data.size.y//1, do: {x, y}
[
to_top,
to_left,
to_right,
to_bottom
]
|> Enum.map(fn line ->
data.map |> count_visible_line_treehouse(line, tree.height)
end)
|> Enum.reduce(&Kernel.*/2)
end
|> Enum.max()
end
defp count_visible_line_treehouse(map, line, limit) do
line
|> Enum.map(fn coordinate -> Map.fetch!(map, coordinate) end)
|> Enum.reduce_while(0, fn
tree, num when tree.height >= limit -> {:halt, num + 1}
_, num -> {:cont, num + 1}
end)
end
end
Trending in Challenges
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
kwando
Yeah, part 2 was not very well defined. I made the same “mistake” as you with not counting smaller trees behind taller ones.. quite annoying.
kwando
Something I keep having use for in these problems where you have to walk around in a matrix is to use a list of “vectors” instead of hardcoding the movements.
This is for part 2, made a more “clever”/convoluted solution for part 1… but I had no use for in part 2.
adamu
Loops in loops in loops
My slowest answer to date this year. I avoided scanning out from every tree, but I did do 4 passes of the forest, and each of those passes does a lot of map lookups, so pretty slow I guess. By slow I mean 40ms.
https://git.adamu.jp/adam/AdventOfCode/src/branch/main/2022/day8.exs
Arzar
Brute forcing it. Now off checking on the subreddit aoc to see how clever people do it
cnschroter
I used Maps of Maps for faster access
Part 1
Part 2
adolfont
Done with Livebook
https://github.com/adolfont/pensandoemelixir/blob/main/adventofcode/2022/day08_2022/day08.livemd
tfwright
Fun with index math. I started the first part with the assumption that visibility is increasing unlikely toward the center so I should go from the edges in. But I made a serious logic error thinking I could get away with just tracking the visibility of the previous row/column as a short hand, forgetting that if a height was equal then I would need to consider the one before that, and the one before that if it was equal…so it took much longer than it should have
After fixing that things went smoothly although I had to do way more debugging in the second part than any previous day. All in all I really enjoyed this one, surveying a matrix from the outside in and then inside out
adamu
There is an optimization you can do in part 1, as you know that once the tree height is 9 you can’t see anything behind that, so there so use checking.
deadbeef
Late to the party. Brute forced like others
https://github.com/ed-flanagan/advent-of-code-solutions-elixir/blob/main/lib/advent/y2022/d08.ex
stevensonmt
Didn’t have a chance to do this before now. Just threw together the quickest brute force solution I could. I’m sure there’s a clever dynamic programming approach that I’m missing. I hate that I couldn’t come up with a more concise way of searching the four directions.
https://github.com/stevensonmt/advent_of_code/blob/2022/2022/day8/lib/day8.ex