Aetherus
I confess that I asked ChatGPT about the math. It gave me a name of an algorithm called the Shoelace formula. I still have to pay attention to the off-by-1 problem though.
Here’s my code (omit the input parsing part):
actions = [{"R", 6}, {"D", 5}, ...]
directions = %{
"L" => {0, -1},
"R" => {0, 1},
"U" => {-1, 0},
"D" => {1, 0}
}
vertices =
for {dir, meters} <- actions,
reduce: [{0, 0}] do
[{i, j} | _] = acc ->
{di, dj} = directions[dir]
next_pos = {i + di * meters, j + dj * meters}
[next_pos | acc]
end
area =
vertices
|> Stream.chunk_every(2, 1, :discard)
|> Stream.map(fn [{i1, j1}, {i2, j2}] ->
(i1 - i2) * (j1 + j2)
end)
|> Enum.sum()
|> div(2)
|> abs()
perimeter = actions |> Enum.map(&elem(&1, 1)) |> Enum.sum()
IO.inspect(area + div(perimeter, 2) + 1)
Part 2 only differs in parsing the input.
Trending in Challenges
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
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
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
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
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
igorb
Unfortunately I didn’t know about the shoelace formula
So here’s my manual solution:
https://github.com/ibarakaiev/advent-of-code-2023/blob/main/lib/advent_of_code/day_18.ex
Logic: similar to Day 10, we can find the inner area by casting a horizontal ray, but since part 2 introduces numbers in hundreds of thousands, filling in the grid would take forever (and probably exhaust my RAM anyway), so I only record corners, i.e. {0, 0, “F”}. Then I group and sort them by the y-coordinate. Then, we have to consider two types of lines: a) lines that don’t have corners, b) lines that have corners.
For lines that have corners, calculate area inside by casting a ray with a modified algorithm from day 10. For lines that don’t, we can infer what they would look like based on the current corners, calculate their area, and multiply by the total number of missing lines. Throughout this, I also needed to keep track of where edges already occured when inferring future lines and calculating the area inside the current line.
I was a bit afraid of accidentally double-counting so I only calculated the inner area and then added the total length of the polygon edges based on the input.
lud
Oh my..
I am 385 LOC and not finished …
I’ll try to finish anyway but man your solution is short.
I can’t grasp the wikipedia page infortunately.
rugyoga
I adapted my code from day 10.
Actually ended up completely rewriting it.
But the concept of storing “L”, “F”, “J”, “7”, “|”, “-” helped me.
Took an hour to run on part 2.
midouest
I did a flood fill for part 1, figured I’d have to implement that ray-casting algorithm for part 2, but got spooked by the large segments. I remembered that folks had mentioned the Shoelace Algorithm for similar problems, so I tried implementing that in Nx. That alone didn’t produce the right answer and I didn’t know enough about the Shoelace Algorithm to tell why, so I converted it to plain Elixir. I had a hunch that the answer was off because the perimeter wasn’t being considered. I don’t fully understand why my math works.
I just divided the perimeter in half and added one plus the area and it was correct. 
Part 1
Part 2
EDIT: Went back and did part 2 with Nx now that I get the math working in plain Elixir.
Part 2 Nx
exists
Wow, the Shoelace formula is something I should learn…
But since I didn’t know about it until now, my solution is – much like others here – a rewrite of the same algorithm as on Day 10: going along horizontal lines, essentially counting places with odd number of crossings from the left. For part 1 I did this directly on the grid, but obviously this didn’t work for part 2, and so I did a complete rewrite. The approach for part 2 is the same, but now moving in chunks. For rows inspecting only the ones with horizontal parts of the boundary, and only one of the “in between” ones. Likewise for columns only check those where there are any vertical parts of the boundary.
The whole thing runs quickly, under 1/10 s, but I am afraid the code is rather unreadable. Apologies
code
Aetherus
It’s a shame that I almost found the shoelace formula myself, but in the end I turned to ChatGPT
When I came back to my thinking, I found some beauty in math.
Suppose you have a convex polygon. You can pick any point inside that polygon and split that polygon into triangles.
For example, in the image above, you can calculate the area of the polygon ABCDEFG by calculating the area of the triangles PAB, PBC, PCD, …, PGA and adding them up. The area of the triangle PAB is
Same for the other triangles.
Be ware that we are doing vector cross product here, so the order matters.
It turns out that this process also works for concave polygons.
In the image above, the area of triangle PCD is
which is negative, but that’s OK because the area of the triangle marked 1 is also in PBC and PDE, so it’s counted twice positive so we need to cancel it once. And the area marked 2 is in PDE but not in the polygon, so we also need to cancel it. The negative area of PCD does both jobs.
It turned out that we can not only pick the P inside the polygon, but anywhere, even outside the polygon or on the edge or corner.
For example, if we choose a P outside the polygon ABCDE, we can always create a polygon that encloses P and shares some of the edges of ABCDE (in the image above, that is the polygon ABCQR). The area of the polygon ABCDE is equal to the area of ABCQR minus the area of AEDCQR. Note that P is both inside ABCQR and AEDCQR, so we can just use the formula above to calculate the area of both polygons.
That’s the proof that even when P is outside the polygon, the formula still works.
If we pick P at (0, 0), then we get the triangular form of the shoelace formula.
As for the trapezoid form of the shoelace formula, though it comes from a very different mindset, if we expand each
(y1 + y2) * (x1 - x2)tox1 * y1 + x1 * y2 - x2 * y1 - x2 * y2, then we notice that, after sum up all the terms, those terms with the same suffixes are canceled out, and the remaining terms are just a permutation of the triangular form.lud
Ah sorry I missed that, I’ll read it carefully later! Thank you!
lud
But how can this work with pixel-like geometries where a point is not actually a point, but has width
Anyway thank you ! I also looked other sources and videos and I have a basic understanding now
woojiahao
TIL about shoelace formula and pick’s theorem:
https://github.com/woojiahao/aoc/blob/main/lib/aoc/y2023/day_18.ex
I felt day 18 was not as interesting since it required some really specific geometry knowledge, but still interesting!
pehbehbeh
I finally took a look at the Shoelace formula and Pick’s Theorem after ignoring it on day 10. After that, Part 1 and 2 are just different inputs.
https://github.com/pehbehbeh/adventofcode/blob/main/2023/18.livemd