Andrew Fontaine

Mostly code

🇨🇦 software engineer, runs on ☕

  • Currently in Sault Ste. Marie, ON
  • Working remotely as a Frontend Engineer for

Cool, asteroids! Not cool, trigonometry!

Let me just search my brain for math I haven’t used in years, no problem at all. Let’s see… if I have one asteroid, x, and two others, y and z, and the angle between x and y is the same as x and z, the only one of y and z is visible. For the first problem, calculating the angles between all the asteroids and eliminating duplicates will show how many other asteroids are visible from a single asteroid.

Read more.

As of today, I’ve beaten my previous year’s record (by one ⭐), but, oh, was it a struggle. The task today is to boost a distress signal to discover what trouble those elves are up to now. First step is to add another parameter mode to the intcode computer! I need to add an offset value to the computer to keep track of the relative mode’s base offset, and add some ways to read the memory utilizing this parameter mode.

  defstruct program: [],
            position: 0,
            input: [],
            output: [],
            offset: 0

  defp parameter(0, x, prog, _computer), do:, x, 0)

  defp parameter(1, x, _prog, _computer), do: x

  defp parameter(2, x, prog, %__MODULE__{offset: offset}), do:, x + offset, 0)

Notice that I have to pass the computer struct into all of these functions so that I can access the offset in the new mode. I think I could alter this here to drop the prog parameter and pull it from the computer struct as well, but I have some other plans to tune up this computer.

Read more.

Saving a mars rover for the elves today! Why do they have to send the password as an image though? Just send me a text, jeez! Anyway, first step is to check and make sure the image was fully received. I can use Stream.chunk_every/2 to turn my list of digits to a list of layers. The only difference is that Stream versions of Enum functions return Streams that are lazily evaluated instead of iterating through the list all at once.

  defp parse_input() do
    |> String.graphemes()

  defp parse_layers(stream, x, y), do: Stream.chunk_every(stream, x * y)

All that’s left is to find the layer that has the least number of 0s, and return the checksum.

Read more.

More intcode computing! Without any modifications to the computer, and a quick search for an algorithm to make permutations, which, like, I just didn’t want to have to figure out, and I am ready to solve this.

  defp permutations([]), do: [[]]

  defp permutations(list),
    do: for(elem <- list, rest <- permutations(list -- [elem]), do: [elem | rest])

Once my permutations are generated, all that’s left is to amp things up.

Read more.

Today, I’m pretty much playing Kerbal Space Program and planning some orbital trajectories. As shown in the problem itself, a tree is the perfect way to represent the orbits, so building one shouldn’t be too hard.

  defp build_tree([], node), do: node

  defp build_tree(list, p) do
    connections = Enum.filter(list, &String.starts_with?(&1, p))

    list = Enum.filter(list, fn x -> x not in connections end)

    c =
      |> x -> x |> String.split(")") |> List.last() end)
      |> x -> build_tree(list, x) end)

    %{p: p, children: c}

list here is the list of connections I’m given, in the form of AAA)BBB, and p is the object that I am finding satellites of. First, I find all the connections by filtering the list for lines that start with the payload, and then for each child I build a tree with that child as the root. Once all the lines are filtered out, the tree is complete.

Read more.
Older Newer