LostKobrakai
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
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
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.That will give you a stream of
{cycle_no, x}tuples that you could use to find the solution to the other parts.inputis 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
Elixir standard library is so awesomely good.
adamu
No standard library for me, just a couple of recursive functions.
Both sub-millisecond. Part 1 especially completes in less than 20 microseconds.
You’re convincing me to look into Stream more for these recursive answers though, 5 parameter 6 clause functions are not the height of readability
https://git.adamu.jp/adam/AdventOfCode/src/branch/main/2022/day10.exs
jmkellenberger
Newbie programmer here, I really enjoyed today’s puzzle. Here’s my attempt.
Day 10
pistelak
I don’t know Elixir that well, but IMHO quite nice, nothing special though: AdventOfCode22/10/aoc/lib/aoc.ex at main · pistelak/AdventOfCode22 · GitHub
stevensonmt
In the end I’m reasonably pleased with my solution but I had a lot of frustration getting there. Had an off by one error in my initial attempt at part 1 that only showed up with the real input and not the sample data. Part 2 was actually much easier but when I pasted the sample data output into vim for testing one of my plugins appended a
#to each line after the first thinking they were meant to be comments. I didn’t realize it and couldn’t understand why my solution was not passing the test. Classic bad input is going to give bad output.kwando
It is probably faster with handrolled recursive functions, but it can be quite dense to read
There are a lot of gems hidden in the standard library:)
stevensonmt
Just curious if this really works:
if Enum.member?(i.x..i.x+2, rem(i.cycle, 40))because I would have thought it needs to be
(i.x - 1)..(i.x + 1)since the sprite position is given by it’s middle pixel position.pistelak
You are right, there is something wrong.
I tried to someone’s else solution and the result is slightly different: 404 File Not Found - Jumpshare … Will take a look.
pistelak
Error by one
- cycles are indexed from 1 but CRT starts drawing at 0. So
Enum.member?(i.x-1..i.x+1, rem(i.cycle - 1, 40))is correct.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 someIO.ANSIto “pretty” print, e.g.https://github.com/ed-flanagan/advent-of-code-solutions-elixir/blob/main/lib/advent/y2022/d10.ex