bjorng

bjorng

Erlang Core Team

This topic is about Day 1 of the Advent of Code 2021.

We have a private leaderboard (shared with users of Erlang Forums):

https://adventofcode.com/2021/leaderboard/private/view/370884

The entry code is:
370884-a6a71927

Showing Posts 1 to 10

bjorng

bjorng OP

Erlang Core Team

Here is my solution in Elixir:

https://github.com/bjorng/advent-of-code-2021/blob/2e585397128f38c3f249623b6392a73fd46e1b87/day01/lib/day01.ex

And over at the Erlang Forums is my solution in Erlang:

code-shoily

code-shoily

Here’s mine:

advent_of_code/day_01.ex at master · code-shoily/advent_of_code (github.com)

Observations:

  • Though wasn’t applicable, but had a re-read of chunk_by and chunk_while-s document. Everytime something needs chunking, I reach out for chunk_by and end up using chunk or chunk_every
  • I had forgotten to use chunk_every’s :discard
  • Livebook + Advent of Code = Elixir is the best way to solve AoC.
Aetherus

Aetherus

My solution using chunk_every/4

Part 1

#!/usr/bin/env elixir

File.stream!("input.txt")
|> Stream.map(&String.trim/1)
|> Stream.map(&String.to_integer/1)
|> Stream.chunk_every(2, 1, :discard)
|> Enum.count(fn [a, b] -> a < b end)
|> IO.inspect()

Part 2

#!/usr/bin/env elixir

File.stream!("input.txt")
|> Stream.map(&String.trim/1)
|> Stream.map(&String.to_integer/1)
|> Stream.chunk_every(3, 1, :discard)
|> Stream.map(&Enum.sum/1)
|> Stream.chunk_every(2, 1, :discard)
|> Enum.count(fn [a, b] -> a < b end)
|> IO.inspect()
code-shoily

code-shoily

omg … why did I not think about a < b and used b - a > 0 instead? Not just the first time but also afterwards when I was looking at it through F# :expressionless:

Aetherus

Aetherus

Actually, I did the same thing in Part 1, and realized that I can just write a < b in Part 2 :smiley:

code-shoily

code-shoily

I took the questions quite literally in my head and on code. I was more obsessed with getting in the leaderboard so I can forgive myself for the first time. But I totally should have seen it the second time.

Also, glad to see your code again @Aetherus … I remember taking help from some of your solutions last year :slight_smile:

Aetherus

Aetherus

Really? I’m flattered. I had a hard time solving problems in the later days of AoC, maybe I need to polish my programming skills more.

code-shoily

code-shoily

I have been randomly solving Advent of Code problems throughout the year, with Elixir. But whenever I see a problem involving 2D arrays or dynamic programming, regardless of practice, I start to panic. One example that comes to mind is Year 2017, Day 3. The idea of solving that gets me nervous lol.

stevensonmt

stevensonmt

TIL Enum.zip_reduce/3

  def parse(path) do
    File.stream!(path)
    |> Enum.map(&String.trim(&1))
    |> Enum.map(&String.to_integer(&1))
  end

  def depth_increment_count(input, step \\ 1) do
    Enum.zip_reduce([input, Enum.drop(input, step)], 0, fn [a, b], acc ->
      if b > a do
        acc + 1
      else
        acc
      end
    end)
  end

  def trios_increment(input) do
    depth_increment_count(input, 3)
  end

Edited for DRYing.

Aetherus

Aetherus

Exactly. I just worked out the answer to part 1. Part 2 is scary. I can’t find a non-brute-force way of solving it.

Where Next? Top

Trending in Challenges Top

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews