bjorng

bjorng

Erlang Core Team

Advent of Code 2021 - Day 6

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

Most Liked

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.

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


jarimatti

jarimatti

I think there is an elegant mathematical way, using linear algebra: the amount of fish that have specific day can be modeled as a vector, where element 0 is the amount of fish that have 0 days left. Total number of fish at any given day is the sum of elements of that vector.

We can multiply that vector with a matrix K, that produces the next day’s fish counts. Here’s what K could look like (apologies for Octave/Matlab syntax, I used that to verify the approach):

K0 = [
  0 0 0 0 0 0 1 0 1;
  1 0 0 0 0 0 0 0 0;
  0 1 0 0 0 0 0 0 0;
  0 0 1 0 0 0 0 0 0;
  0 0 0 1 0 0 0 0 0;
  0 0 0 0 1 0 0 0 0;
  0 0 0 0 0 1 0 0 0;
  0 0 0 0 0 0 1 0 0;
  0 0 0 0 0 0 0 1 0
];

# Fun fact: matrix for computing any initial condition to day 80 is:
K80 = K0 ^ 80;

# Get day 1 from day 0 counts per day:
v1 = v0 * K;

# Get day 80 from day 0 counts per day:
v80 = v0 * K80;

Full code here for my input: aoc2021_day6.m

I’ve yet to figure out how to do this in Elixir fluently and it seems it’s time to learn some Nx. :grin:

Where Next?

Popular in Challenges Top

igorb
So… that’s it? Everyone is stuck on part 2? :slight_smile: I looked at Reddit hints and thought I probably wouldn’t have come up with the...
New
jkwchui
Monkeys fitted squarely as GenServers in my head. My initial problem was using cast instead of call; I imagine impolite monkeys slinging...
New
sb8244
Note: This topic is to talk about Day 10 of the Advent of Code 2019 . There is a private leaderboard for elixirforum members. You can jo...
New
bjorng
Note: This topic is to talk about Day 18 of the Advent of Code 2019. There is a private leaderboard for elixirforum members. You can joi...
New
Aetherus
This topic is about Day 15 of the Advent of Code 2020 . Thanks to @egze, we have a private leaderboard: https://adventofcode.com/2020/l...
New
groovyda
Today’s challenge for me was about using reduce: defmodule Prob5 do def move([[h1 | rest] = _list1, list2]) do [rest, [h1 | list2]...
New
shritesh
I mapped both the cards and every possible hand to numeric values and sorted them. In part 2 I could only think of replacing the jokers w...
New
bjorng
Note: This topic is to talk about Day 4 of the Advent of Code 2019. There is a private leaderboard for elixirforum members. You can join...
New
mattbaker
I’m having so much fun working on the “Protohackers” challenges, I never got into Advent of Code much but this has been amazing. The chal...
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

josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39247 209
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35953 110
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I'm a nov...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
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