bjorng

bjorng

Erlang Core Team

This topic is about Day 6 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

ruslandoga

ruslandoga

I went with representing the frequencies as a tuple and a simulation step was:

defp simulate({s0, s1, s2, s3, s4, s5, s6, s7, s8}, days) when days > 0 do
  simulate({s1, s2, s3, s4, s5, s6, s7 + s0, s8, s0}, days - 1)
end

Full solution

It’d be nice to see an Nx solution here, it fits this problem very nicely.

code-shoily

code-shoily

I did it the “slow” way the first time and got instant gratification, the later part, not so instant (in fact not all) until I remembered bucket sort :slight_smile: and my good old friends Enum.frequencies and Map.merge/3

Here’s how the end product looked like:

defmodule AdventOfCode.Y2021.Day06 do
  use AdventOfCode.Helpers.InputReader, year: 2021, day: 6

  def run_1, do: input!() |> parse() |> multiply(80) |> Enum.sum()
  def run_2, do: input!() |> parse() |> multiply(256) |> Enum.sum()
  def parse(f), do: f |> String.split(",") |> Enum.map(&String.to_integer/1) |> Enum.frequencies()

  def multiply(fishes, day) do
    (day == 0 && Map.values(fishes)) ||
      multiply(
        Map.pop(fishes, 0)
        |> then(
          &Map.merge(
            for({k, v} <- elem(&1, 1), into: %{}, do: {k - 1, v}),
            %{6 => elem(&1, 0) || 0, 8 => elem(&1, 0) || 0},
            fn _, a, b -> a + b end
          )
        ),
        day - 1
      )
  end
end
stevensonmt

stevensonmt

struggling with the size of part 2. I want to do this in a mathematical way following an exponential growth equation like number of fishes * (1 + rate of growth)^number of days but this doesn’t work.

wasi0013

wasi0013

I enjoyed this one! :smiley:

defmodule Aoc.Y2021.Day06 do
  @moduledoc """
  Solved https://adventofcode.com/2021/day/6
  """
  import Aoc.Helper.IO

  def run_part1(), do: get_input() |> solve_part1()
  def run_part2(), do: get_input() |> solve_part2()

  def solve_part1(data), do: solve(data, 80)
  def solve_part2(data), do: solve(data, 256)

  def solve(data, days), do: data |> fish_count() |> simulate(days) |> Enum.sum()
  def fish_count(data), do: Enum.map(0..8, fn n -> Map.get(Enum.frequencies(data), n, 0) end)

  def simulate(fish_count, 0), do: fish_count

  def simulate([zeroth, first, second, third, fourth, fifth, sixth, seventh, eighth], days) do
    simulate([first, second, third, fourth, fifth, sixth, seventh + zeroth, eighth, zeroth], days - 1)
  end

  defp get_input(), do: get_integer_input("2021", "06", ",")
end


code-shoily

code-shoily

I wish I analyzed input properties more before jumping to solving things. The same happened with Bingo, the solution looked different in my head before and after I realized it’s a 5x5 grid all the time.

code-shoily

code-shoily

Looking forward to what you come up with. I was thinking the same thing but got lazy (scared) and backed away.

stevensonmt

stevensonmt

I’m going to have to go to bed without solving it. My initial idea was Bn= B0 x 2^Kt where B0 is the original count for each timer value. K is 1/7 and t (for part 2) is 256 - the original timer value. Just calculate that for each original timer value and then add them together. Not sure how to make it work though.

DERP.
Not accounting for the maturation time with each generation.

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