LostKobrakai

LostKobrakai

Advent of Code 2022 - Day 10

This one was really fun contrary to other “implement something CPU like” puzzles in the past. I used a bunch of Stream API and a module for scoping the CRT logic. I really liked the visual component of part 2.

Solution
defmodule Day10 do
  defmodule CRT do
    defstruct pixels: [], index: 0

    def render_pixel(state, x) do
      pixel = if state.index in (x - 1)..(x + 1), do: "#", else: "."

      %__MODULE__{
        pixels: [pixel | state.pixels],
        index: rem(state.index + 1, 40)
      }
    end

    def render(state) do
      state.pixels
      |> Enum.reverse()
      |> Enum.chunk_every(40)
      |> Enum.join("\n")
    end
  end

  def run(text) do
    text
    |> program()
    |> Stream.filter(fn {_, cycle} -> cycle in [20, 60, 100, 140, 180, 220] end)
    |> Stream.map(fn {x, cycle} -> x * cycle end)
    |> Enum.take(6)
    |> Enum.sum()
  end

  def render_to_crt(text) do
    text
    |> program()
    |> Stream.take_while(fn {_, cycle} -> cycle <= 240 end)
    |> Enum.reduce(%CRT{}, fn {x, _}, crt ->
      CRT.render_pixel(crt, x)
    end)
    |> CRT.render()
  end

  defp program(text) do
    text
    |> String.split("\n", trim: true)
    |> Stream.transform(1, fn
      "noop", x -> {[x], x}
      "addx " <> num, x -> {[x, x], x + String.to_integer(num)}
    end)
    |> Stream.with_index(1)
  end
end

Most Liked

deadbeef

deadbeef

Nothing special/similar to others. Reminded me of 2021-13 which also had you print out letters with .'s and #'s. So had some fun and added some IO.ANSI to “pretty” print, e.g.

https://github.com/ed-flanagan/advent-of-code-solutions-elixir/blob/main/lib/advent/y2022/d10.ex

kwando

kwando

I had a very clunky version that got me the gold stars, but after some coffee I ended up with a super neat version using Stream.transform.

  input
  |> Stream.transform({1, 1}, fn 
    :noop, {cycle, x} ->
      {[{cycle, x}], {cycle + 1, x}}
    {:addx, value}, {cycle, x} ->
      {[{cycle, x}, {cycle + 1, x}], {cycle + 2, x + value}}
  end)

That will give you a stream of {cycle_no, x} tuples that you could use to find the solution to the other parts. input is parsed in to a list of tuples, the small example would look like the [:noop, {:addx, 3}, {:addx, -5}].

Just pipe that stream into this for the second part :slight_smile:

cycle_stream # from above
|> Stream.map(fn 
  {pc, x} when rem(pc-1, 40) in (x-1)..(x+1) -> ?#
  _ -> ?\s
end)
|> Stream.chunk_every(40)
|> Enum.intersperse(?\n)
|> IO.puts

Elixir standard library is so awesomely good.

kwando

kwando

It is probably faster with handrolled recursive functions, but it can be quite dense to read :sweat_smile:

There are a lot of gems hidden in the standard library:)

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