bjorng

bjorng

Erlang Core Team

Advent of Code 2025 - Day 12

My solution first checks whether the presents can fit into a region based on area alone. Turns out that eliminated more than half of the regions. I then started to design in my head a brute-force algorithm to check the other regions. Before implementing it, though, I thought that I should check my solution on the Advent of Code site. I thought that I would get “this is not correct; it is too high”, but it turned out to be the correct answer.

defmodule Day12 do
  def part1(input) do
    {presents, regions} = parse(input)

    regions
    |> Enum.reject(fn region ->
      presents_fit?(region, presents)
    end)
    |> Enum.count
  end

  defp presents_fit?(region, presents) do
    num_units = Enum.map(presents, fn {index, [shape | _]} ->
      Enum.reduce(shape, [], &(&1++&2))
      |> Enum.count(&(&1 === ?\#))
      |> then(&{index, &1})
    end)
    |> Map.new

    {{width, height}, qs} = region
    area = width * height

    qs
    |> Enum.with_index
    |> Enum.map(fn {q, index} ->
      q * Map.get(num_units, index)
    end)
    |> Enum.sum
    |> then(fn needed ->
      if needed > area do
        # Can't possibly fit.
        true
      else
        # Might fit; must check.
        brute_force_solution(region, presents)
      end
    end)
  end

  defp brute_force_solution(_region, _presents) do
    # Turns out that for all inputs the presents will always fit.
    false
  end

  defp rot(grid) do
    Enum.zip_with(grid, &Enum.reverse/1)
  end

  defp hflip(grid) do
    Enum.map(grid, &Enum.reverse/1)
  end

  defp vflip(grid) do
    Enum.reverse(grid)
  end

  defp transpose(list) do
    Enum.zip_with(list, &Function.identity/1)
  end

  defp parse(input) do
    {presents, [regions]} = Enum.split(input, length(input) - 1)

    presents = Enum.map(presents, fn present ->
      [index | grid] = String.split(present, "\n", trim: true)

      grid = Enum.map(grid, &String.to_charlist/1)

      grids = Enum.flat_map_reduce(1..4, grid, fn _, grid ->
        {[grid, hflip(grid), vflip(grid)], rot(grid)}
      end)
      |> elem(0)
      |> Enum.uniq

      index = index
      |> String.trim(":")
      |> String.to_integer

      if true do
        IO.puts("#{index}:")
        grids
        |> transpose
        |> Enum.each(fn grid ->
          Enum.each(grid, &IO.write("#{&1}  "))
          IO.puts ""
        end)
        IO.puts ""
      end

      {index, grids}
    end)

    regions = regions
    |> String.split("\n", trim: true)
    |> Enum.map(fn region ->
      [size, qs] = String.split(region, ": ")

      size = size
      |> String.split("x")
      |> Enum.map(&String.to_integer(&1))
      |> List.to_tuple

      qs = String.split(qs, " ")
      |> Enum.map(&String.to_integer(&1))
      {size, qs}
    end)
    |> Enum.sort

    {presents, regions}
  end
end

Most Liked

rvnash

rvnash

Yeah, like others, my “solution” isn’t worth posting here because it only checks to see if the number of tiles can fit in provided area. It doesn’t look at shapes, flips, or rotations. Good enough, I’m done.

I just wanted to thank @lud , @bjorng , @hauleth and others here for providing your code for me to look at. All of you have such great and elegant understanding of functional programming and Elixir, I feel like I learn a lot reading your code. Now if I could only apply what I learn in my own code :wink: . Anyway thanks.

Someday, after the holidays, I’m going to go back to Day 10 Part 2. That’s the only one I could not crack on my own, having to resort to a third-party solver. I have some more ideas that I want to try.

Happy Holidays!

bjorng

bjorng

Erlang Core Team

I just designed it in my head. I never implemented it, but I think would have worked and it might be fun to implement.

No, I don’t think there are billions and billions of possibilities.

My idea was to do a breadth first search using a priority queue. The frontier would be the unoccupied positions next to all previously placed shapes, and it would start out at a corner. The priority queue is ordered by the area covered by all previously placed shapes. Therefore, if a solution is found, it will be a solution that covers the least number of units.

There are not always eight possible orientations. One shape had only two possible orientations, three had four orientations, and two had eight (for my input). Some orientations can be excluded when placed if there would be an embedded unreachable unit.

According to the page you linked to, the problem is NP-hard if the rectangles being packed have varying width and height. As far as I understand, packing identical rectangles in a rectangle is not NP-hard.

lud

lud

And for each state in the queue you must enqueue a new state for every piece*permutation. It can grow quickly. In aoc in general I just keep 10k or 20k items in the queue and it works.

I found some people with the actual solution claiming less than 3 minutes runtime in python so I guess it’s not that hard. Gosh I’m way too lazy to give it a try now :slight_smile:

Where Next?

Popular in Challenges Top

maennchen
Ok, that was a rough one today. I haven’t found a way to improve the algorithm further. Part 1 runs in .5 seconds, Part 2 in ~ 5 minutes...
New
bjorng
This topic is about Day 5 of the Advent of Code 2021. We have a private leaderboard (shared with users of Erlang Forums ): https://adve...
New
coen.bakker
Since I started using Elixir, I have benefited greatly from being able to study various open-source projects. The codebase of LiveBook, i...
New
liamcmitchell
A frustrating one for me. I spent a long time trying to understand why some combinations resulted in fewer presses and struggled to keep ...
New
New
connorlay
Note by the Moderators: This topic is for general discussion about the Advent of Code 2018. To prevent people from being spoiled about s...
New
stevensonmt
Anyone else think the prompt for this challenge is contradictory? The rules for comparing packets include If both values are lists, c...
New
lud
Gosh this one took me sooo much time. At first I was trying to iterate each digit independently on the input A number to make digits cha...
New
bjorng
Note: This topic is to talk about Day 23 of the Advent of Code. For general discussion about the Advent of Code 2018 and links to topics...
New
christhekeele
Setting this down for the night, as after a quick naive solve for quick part 1 I realize that part 2 is by design computationally expensi...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement